• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua [TFS 1.x] addEvent executing twice?

  • Thread starter Thread starter Evil Puncker
  • Start date Start date
E

Evil Puncker

Guest
Hi everyone, I have this code to check every SQM that have action ID 2130 every 60 seconds to see if player storage is still valid, it works very well, but once it teleport me, the line 7 is executed twice, why?


LUA:
local event = {}
local function checkSQM(cid)
    local player = Player(cid)
    if player:getStorageValue(1234) < os.time() then
        event[cid] = nil
        player:teleportTo(Position(326, 300, 7))
        player:popupFYI("<msg>")
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    else
        event[cid] = addEvent(checkSQM, 60 * 1000, cid)
    end
        event[cid] = nil
end

local moveevent = MoveEvent("example")
 
function moveevent.onStepIn(creature, item, position, fromPosition)
    if creature:isPlayer() then
        checkSQM(creature:getId())
    end
end
moveevent:aid(2130)
moveevent:register()
 
Solution
LUA:
local moveevent = MoveEvent("example")





LUA:
local event = {}

local function checkSQM(cid)
    local player = Player(cid)

    -- if called by addEvent 60 seconds later and player is gone?
    if (not player) then return end

    -- will always execute first time a player uses unless you are setting store:1234 elsewhere
    -- cuz storage defaults to -1 right?
    if player:getStorageValue(1234) < os.time() then
        stopEvent(event[cid]) -- perhaps? I don't see why you'd need to nil out event[cid] unless you...
LUA:
local moveevent = MoveEvent("example")





LUA:
local event = {}

local function checkSQM(cid)
    local player = Player(cid)

    -- if called by addEvent 60 seconds later and player is gone?
    if (not player) then return end

    -- will always execute first time a player uses unless you are setting store:1234 elsewhere
    -- cuz storage defaults to -1 right?
    if player:getStorageValue(1234) < os.time() then
        stopEvent(event[cid]) -- perhaps? I don't see why you'd need to nil out event[cid] unless you think the GC is trash.
        player:teleportTo(Position(326, 300, 7))
        player:popupFYI("<msg>")
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    else
        event[cid] = addEvent(checkSQM, 60 * 1000, cid)
        return -- maybe you meant to do this ?????????????
    end
    -- Maybe you mean this?
    stopEvent(event[cid])
end

local gadsenSnake = MoveEvent("TWO_VOWELS_ADJACENT_FROM_SEPARATE_WORDS_REEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
function gadsenSnake.onStepIn(creature, item, position, fromPosition)
    if creature:isPlayer() then
        checkSQM(creature:getId())
    end
end
gadsenSnake:aid(2130)
gadsenSnake:register()
 
Solution
LUA:
local moveevent = MoveEvent("example")





LUA:
local event = {}

local function checkSQM(cid)
    local player = Player(cid)

    -- if called by addEvent 60 seconds later and player is gone?
    if (not player) then return end

    -- will always execute first time a player uses unless you are setting store:1234 elsewhere
    -- cuz storage defaults to -1 right?
    if player:getStorageValue(1234) < os.time() then
        stopEvent(event[cid]) -- perhaps? I don't see why you'd need to nil out event[cid] unless you think the GC is trash.
        player:teleportTo(Position(326, 300, 7))
        player:popupFYI("<msg>")
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    else
        event[cid] = addEvent(checkSQM, 60 * 1000, cid)
        return -- maybe you meant to do this ?????????????
    end
    -- Maybe you mean this?
    stopEvent(event[cid])
end

local gadsenSnake = MoveEvent("TWO_VOWELS_ADJACENT_FROM_SEPARATE_WORDS_REEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
function gadsenSnake.onStepIn(creature, item, position, fromPosition)
    if creature:isPlayer() then
        checkSQM(creature:getId())
    end
end
gadsenSnake:aid(2130)
gadsenSnake:register()

apparently players can start infinite addEvent by stepping in the tile multiple times... I tried addressing it with an OnStepOut event calling stopEvent(event[cid]) but no success, any idea?
 
apparently players can start infinite addEvent by stepping in the tile multiple times... I tried addressing it with an OnStepOut event calling stopEvent(event[cid]) but no success, any idea?
Add one more storage, before event start
LUA:
llocal event = {}

local function checkSQM(cid)
    local player = Player(cid)

    -- if called by addEvent 60 seconds later and player is gone?
    if (not player) then return end

    -- will always execute first time a player uses unless you are setting store:1234 elsewhere
    -- cuz storage defaults to -1 right?
    if player:getStorageValue(1234) < os.time() and player:getStorageValue(2255) == 1 then
        stopEvent(event[cid]) -- perhaps? I don't see why you'd need to nil out event[cid] unless you think the GC is trash.
        player:teleportTo(Position(326, 300, 7))
        player:setStorageValue(2255, 0)
        player:popupFYI("<msg>")
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    else
        event[cid] = addEvent(checkSQM, 60 * 1000, cid)
        return -- maybe you meant to do this ?????????????
    end
    -- Maybe you mean this?
    stopEvent(event[cid])
end

local gadsenSnake = MoveEvent("TWO_VOWELS_ADJACENT_FROM_SEPARATE_WORDS_REEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
function gadsenSnake.onStepIn(creature, item, position, fromPosition)
    if creature:isPlayer() and player:getStorageValue(2255) == 0 then
        checkSQM(creature:getId())
        player:setStorageValue(2255, 1)
    end
end
gadsenSnake:aid(2130)
gadsenSnake:register()

Something like this?

Maybe after else and stopevent should add player:setStorageValue(2255, 0) aswell? Test it ;p
 
Back
Top