• 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!

how to stop addEvent if player died?

vexler222

Active Member
Joined
Apr 22, 2012
Messages
714
Solutions
15
Reaction score
46
Hi, i try make simple arena quest with 10 waves, but i don't know how to stop addEvent and reset "check" storage if player died, can someone help me?

Lua:
local kb = Action()
local topL = Position(3722, 2927, 7)
local botR = Position(3730, 2935, 7)
function kb.onUse(player, item, position, creature)
    if player:getStorageValue(reward) > 0 then
        player:teleportTo(Position(3711, 2930, 4))
        return false
    end
    if player:getStorageValue(check) < 1 then
        player:setStorageValue(check, 1)

        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Wave 1]")
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
        end, 10000)
        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Wave 2]")
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
        end, 21000)
        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Wave 3]")
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
        end, 32000)
        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Wave 4]")
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
        end, 43000)
        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Wave 5]")
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
        end, 54000)
        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Wave 6]")
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
        end, 65000)
        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Wave 7]")
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
        end, 76000)
        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Wave 8]")
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
        end, 87000)
        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Wave 9]")
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
        end, 98000)
        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[Wave 10 (LAST)]")
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
        end, 109000)
        addEvent(function()
            player:setStorageValue(reward, 1)
            player:teleportTo(Position(3711, 2930, 4))

        end, 140000)
    else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Bruh Bruh")
        player:teleportTo(Position(3723, 2923, 7))
    end
end

kb:uid(14785)
kb:register()
 
Solution
as @sharinn said you should save the ids in a table for each player and then use the table to stop the events.
example with your script:
Lua:
local topL = Position(3722, 2927, 7)
local botR = Position(3730, 2935, 7)
local waves = 10
local eventIds = {}
local stopEvents = function (player)
    for _, eventID in pairs(eventIds[player:getId()] or {}) do
        stopEvent(eventID)
    end
    return true
end

local kb = Action()

function kb.onUse(player, item, position, creature)
    if player:getStorageValue(reward) > 0 then
        player:teleportTo(Position(3711, 2930, 4))
        return false
    end

    if player:getStorageValue(check) < 1 then
        player:setStorageValue(check, 1)
        player:registerEvent("kbDeath")...
Here is a part of my script that does that, should give you an idea, not only for that, but also how to use addEvent.

Lua:
local function checkTp(playerId, itemid, seconds)
    local player = Player(playerId)
    if not player or not player:isPlayer() then
        return nil
    end
    
    if player:isPzLocked() then
        return nil
    end
    
    if seconds == 0 then
        teleportPlayer(player.uid, itemid)
        return true
    end
    
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    player:say(seconds, TALKTYPE_MONSTER_SAY)
    addEvent(checkTp, 1000, player.uid, itemid, seconds-1)
    return true
end
 
Here is a part of my script that does that, should give you an idea, not only for that, but also how to use addEvent.

Lua:
local function checkTp(playerId, itemid, seconds)
    local player = Player(playerId)
    if not player or not player:isPlayer() then
        return nil
    end
  
    if player:isPzLocked() then
        return nil
    end
  
    if seconds == 0 then
        teleportPlayer(player.uid, itemid)
        return true
    end
  
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    player:say(seconds, TALKTYPE_MONSTER_SAY)
    addEvent(checkTp, 1000, player.uid, itemid, seconds-1)
    return true
end
I tried to use your fragment with playerId, but still not work for me, all waves starting when player died and on last addevent he got tp to reward room, maybe im using it wrong
You only need to add at the beginning of the script "if not isplayer(cid) then return false" this should stop the event
It can't work i think, because if player died, and login again, he still exist
 
add onLogout event that stop player event (will be nice to store events in table, index should be player uid/guid
can you write tfs version, on revscript will be really easy, on xml version idk if 2 scripts can be in single file onUse and onLogout
...
you can create global player table with events but i don't recommend this solution, local access should be enough
 
as @sharinn said you should save the ids in a table for each player and then use the table to stop the events.
example with your script:
Lua:
local topL = Position(3722, 2927, 7)
local botR = Position(3730, 2935, 7)
local waves = 10
local eventIds = {}
local stopEvents = function (player)
    for _, eventID in pairs(eventIds[player:getId()] or {}) do
        stopEvent(eventID)
    end
    return true
end

local kb = Action()

function kb.onUse(player, item, position, creature)
    if player:getStorageValue(reward) > 0 then
        player:teleportTo(Position(3711, 2930, 4))
        return false
    end

    if player:getStorageValue(check) < 1 then
        player:setStorageValue(check, 1)
        player:registerEvent("kbDeath")
        player:registerEvent("kbLogout")
        stopEvents(player)
        local playerId = player:getId()
        eventIds[playerId] = {}
        for i = 1, waves do
            eventIds[playerId][i] = addEvent(function (playerId)
                local player = Player(playerId)
                if not player then
                    return
                end

                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("[Wave %d%s]", i, i == waves and " (LAST)" or ""))
                Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
                Game.createMonster("Demon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
                Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
                Game.createMonster("Dragon", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
                Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
                Game.createMonster("Dragon Lord", Position(math.random(topL.x, botR.x), math.random(topL.y, botR.y), topL.z))
            end, 10000 + ((i-1) * 1000), playerId)
        end

        eventIds[playerId]['reward'] = addEvent(function (playerId)
            local player = Player(playerId)
            if not player then
                return
            end

            player:setStorageValue(reward, 1)
            player:teleportTo(Position(3711, 2930, 4))
        end, 140000, playerId)
        return true
    end

    player:sendTextMessage(MESSAGE_STATUS_WARNING, "Bruh Bruh")
    player:teleportTo(Position(3723, 2923, 7))
    return true
end

kb:uid(14785)
kb:register()

local kbDeath = CreatureEvent("kbDeath")
kbDeath.onDeath = stopEvents
kbDeath:register()

local kbLogout = CreatureEvent("kbLogout")
kbLogout.onLogout = stopEvents
kbLogout:register()
 
Last edited:
Solution
you can use creature ID instead of Unique ID, cause it changes everytime that player dies/relog, so if It dies and you make a verification for Player(creatureId) you'll not have a player and it'll stop the script I guess, not sure tho, but I use something like this in my server
 
Back
Top