• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved TFS 1.1 How to get new position

Azze19

Solo developer
Joined
Jul 4, 2014
Messages
75
Reaction score
23
I want to make an area directional spell, however, I want it to stop midway if the player moves; heres the script:

local config = {
interval = 200,
}

function onSay(cid)
local player = Player(cid)
local playerPos = player:getPosition()
local playerDir = player:getDirection()
local cT = config
local posT = {}

if playerDir == 0 then
posT[1] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
posT[2] = {x=playerPos.x , y=playerPos.y-1, z=playerPos.z}
posT[3] = {x=playerPos.x-1 , y=playerPos.y-1, z=playerPos.z}
posT[4] = {x=playerPos.x , y=playerPos.y-1, z=playerPos.z}
posT[5] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
elseif playerDir == 1 then
posT[1] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
posT[2] = {x=playerPos.x+1 , y=playerPos.y, z=playerPos.z}
posT[3] = {x=playerPos.x+1 , y=playerPos.y+1, z=playerPos.z}
posT[4] = {x=playerPos.x+1 , y=playerPos.y, z=playerPos.z}
posT[5] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
elseif playerDir == 2 then
posT[1] = {x=playerPos.x+1 , y=playerPos.y+1, z=playerPos.z}
posT[2] = {x=playerPos.x , y=playerPos.y+1, z=playerPos.z}
posT[3] = {x=playerPos.x-1 , y=playerPos.y+1, z=playerPos.z}
posT[4] = {x=playerPos.x , y=playerPos.y+1, z=playerPos.z}
posT[5] = {x=playerPos.x+1 , y=playerPos.y+1, z=playerPos.z}
elseif playerDir == 3 then
posT[1] = {x=playerPos.x-1 , y=playerPos.y-1, z=playerPos.z}
posT[2] = {x=playerPos.x-1 , y=playerPos.y, z=playerPos.z}
posT[3] = {x=playerPos.x-1 , y=playerPos.y+1, z=playerPos.z}
posT[4] = {x=playerPos.x-1 , y=playerPos.y, z=playerPos.z}
posT[5] = {x=playerPos.x-1 , y=playerPos.y-1, z=playerPos.z}
end

for times, pos in pairs (posT) do
local delay = cT.interval * times
local newplypos = player:getPosition()

if playerPos ~= newplypos then
break
else
addEvent(function() Position(pos):sendMagicEffect(10) end, delay)
-- addEvent(dealDamagePos, delay, cid, pos, 1, PHYSICAL, 50, 200, 4)
end
end
end

I figured that once you get the player's position the value is set, but I was wondering if there's a way to refresh that value so that the for can break if the player moves.

Also I'm having issues with the dealDamagePos on the second addEvent, if someone could help me figure this out it'll be a plus. Thanks!
 
I edited the config table a bit as well:

Code:
local config = {
    interval = 200,
    effect = CONST_ME_HITAREA, -- What type of Magic Effect should the spell have?
    stopOnChangePosition = true, -- Should the spell effect stop if the player moves?
    stopOnChangeDirection = true -- Should the spell effect stop if the player moves?
}

-- checkPlayerStatus(OriginalPosition, NewPosition, OriginalDirection, NewDirection)
local function checkPlayerStatus(pos1, pos2, dir1, dir2)
    if config.stopOnChangePosition then
        if pos1 ~= pos2 then
            print('positions do not match')
            return false
        end
    end
  
    if config.stopOnChangeDirection then
        if dir1 ~= dir2 then
            print('direction was changed')
            return false
        end
    end
return true
end

-- sendDamageEffect(effectPosition, playerPosition, playerDirection, playerID)
local function sendDamageEffect(effectPos, playerOrigPos, playerOrigDir, playerID)
    local player = Player(playerID)
    if not player then
        return
    end
    local newPos = player:getPosition()
    local newDir = player:getDirection()
  
    if checkPlayerStatus(playerOrigPos, newPos, playerOrigDir, newDir) then
        Position(effectPos):sendMagicEffect(config.effect)
    end
end

function onSay(cid)
    local player = Player(cid)
    local playerPos = player:getPosition()
    local playerDir = player:getDirection()
    local cT = config
    local posT = {}

    if playerDir == 0 then
        posT[1] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
        posT[2] = {x=playerPos.x , y=playerPos.y-1, z=playerPos.z}
        posT[3] = {x=playerPos.x-1 , y=playerPos.y-1, z=playerPos.z}
        posT[4] = {x=playerPos.x , y=playerPos.y-1, z=playerPos.z}
        posT[5] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
    elseif playerDir == 1 then
        posT[1] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
        posT[2] = {x=playerPos.x+1 , y=playerPos.y, z=playerPos.z}
        posT[3] = {x=playerPos.x+1 , y=playerPos.y+1, z=playerPos.z}
        posT[4] = {x=playerPos.x+1 , y=playerPos.y, z=playerPos.z}
        posT[5] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
    elseif playerDir == 2 then
        posT[1] = {x=playerPos.x+1 , y=playerPos.y+1, z=playerPos.z}
        posT[2] = {x=playerPos.x , y=playerPos.y+1, z=playerPos.z}
        posT[3] = {x=playerPos.x-1 , y=playerPos.y+1, z=playerPos.z}
        posT[4] = {x=playerPos.x , y=playerPos.y+1, z=playerPos.z}
        posT[5] = {x=playerPos.x+1 , y=playerPos.y+1, z=playerPos.z}
    elseif playerDir == 3 then
        posT[1] = {x=playerPos.x-1 , y=playerPos.y-1, z=playerPos.z}
        posT[2] = {x=playerPos.x-1 , y=playerPos.y, z=playerPos.z}
        posT[3] = {x=playerPos.x-1 , y=playerPos.y+1, z=playerPos.z}
        posT[4] = {x=playerPos.x-1 , y=playerPos.y, z=playerPos.z}
        posT[5] = {x=playerPos.x-1 , y=playerPos.y-1, z=playerPos.z}
    end

    for times, pos in pairs (posT) do
        local delay = cT.interval * times
        addEvent(sendDamageEffect, delay, pos, playerPos, playerDir, player:getId())
        -- addEvent(dealDamagePos, delay, cid, pos, 1, PHYSICAL, 50, 200, 4)
    end
end
 
I edited the config table a bit as well:

Code:
local config = {
    interval = 200,
    effect = CONST_ME_HITAREA, -- What type of Magic Effect should the spell have?
    stopOnChangePosition = true, -- Should the spell effect stop if the player moves?
    stopOnChangeDirection = true -- Should the spell effect stop if the player moves?
}

-- checkPlayerStatus(OriginalPosition, NewPosition, OriginalDirection, NewDirection)
local function checkPlayerStatus(pos1, pos2, dir1, dir2)
    if config.stopOnChangePosition then
        if pos1 ~= pos2 then
            print('positions do not match')
            return false
        end
    end
 
    if config.stopOnChangeDirection then
        if dir1 ~= dir2 then
            print('direction was changed')
            return false
        end
    end
return true
end

-- sendDamageEffect(effectPosition, playerPosition, playerDirection, playerID)
local function sendDamageEffect(effectPos, playerOrigPos, playerOrigDir, playerID)
    local player = Player(playerID)
    if not player then
        return
    end
    local newPos = player:getPosition()
    local newDir = player:getDirection()
 
    if checkPlayerStatus(playerOrigPos, newPos, playerOrigDir, newDir) then
        Position(effectPos):sendMagicEffect(config.effect)
    end
end

function onSay(cid)
    local player = Player(cid)
    local playerPos = player:getPosition()
    local playerDir = player:getDirection()
    local cT = config
    local posT = {}

    if playerDir == 0 then
        posT[1] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
        posT[2] = {x=playerPos.x , y=playerPos.y-1, z=playerPos.z}
        posT[3] = {x=playerPos.x-1 , y=playerPos.y-1, z=playerPos.z}
        posT[4] = {x=playerPos.x , y=playerPos.y-1, z=playerPos.z}
        posT[5] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
    elseif playerDir == 1 then
        posT[1] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
        posT[2] = {x=playerPos.x+1 , y=playerPos.y, z=playerPos.z}
        posT[3] = {x=playerPos.x+1 , y=playerPos.y+1, z=playerPos.z}
        posT[4] = {x=playerPos.x+1 , y=playerPos.y, z=playerPos.z}
        posT[5] = {x=playerPos.x+1 , y=playerPos.y-1, z=playerPos.z}
    elseif playerDir == 2 then
        posT[1] = {x=playerPos.x+1 , y=playerPos.y+1, z=playerPos.z}
        posT[2] = {x=playerPos.x , y=playerPos.y+1, z=playerPos.z}
        posT[3] = {x=playerPos.x-1 , y=playerPos.y+1, z=playerPos.z}
        posT[4] = {x=playerPos.x , y=playerPos.y+1, z=playerPos.z}
        posT[5] = {x=playerPos.x+1 , y=playerPos.y+1, z=playerPos.z}
    elseif playerDir == 3 then
        posT[1] = {x=playerPos.x-1 , y=playerPos.y-1, z=playerPos.z}
        posT[2] = {x=playerPos.x-1 , y=playerPos.y, z=playerPos.z}
        posT[3] = {x=playerPos.x-1 , y=playerPos.y+1, z=playerPos.z}
        posT[4] = {x=playerPos.x-1 , y=playerPos.y, z=playerPos.z}
        posT[5] = {x=playerPos.x-1 , y=playerPos.y-1, z=playerPos.z}
    end

    for times, pos in pairs (posT) do
        local delay = cT.interval * times
        addEvent(sendDamageEffect, delay, pos, playerPos, playerDir, player:getId())
        -- addEvent(dealDamagePos, delay, cid, pos, 1, PHYSICAL, 50, 200, 4)
    end
end


Thank you! that did it :)
 
Back
Top