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

Lua addEvent problem

Tubeshop

Member
Joined
Nov 23, 2023
Messages
173
Reaction score
19
I have a problem with the addEvent function. I want to create a system where the player gets rewards for every 15 seconds in the arena. the problem is that if a player enters and exits the arena and repeats this action 3 times, he will receive 3 prizes every 15 seconds. I don't know how to kill addevent after player exits. the exit also changes the storage to the one I have in the script, but when the player enters the arena again, the storage is correct and he gets 3 prizes. How to kill an add event after a player leaves the arena? I tried to assign addevent to a variable and do stopevent on this variable, but it doesn't work either. please help

addEvent(StayArena, 15000, player:getId())
function StayArena(playerId)

local player = Player(playerId)
if not player then
return
end

if(player:getStorageValue(60000)) <= 0 then
return
end

-- add reward here
addEvent(StayArena, 15000, playerId)

end
 
Last edited:
I have a problem with the addEvent function. I want to create a system where the player gets rewards for every 15 seconds in the arena. the problem is that if a player enters and exits the arena and repeats this action 3 times, he will receive 3 prizes every 15 seconds. I don't know how to kill addevent after player exits. the exit also changes the storage to the one I have in the script, but when the player enters the arena again, the storage is correct and he gets 3 prizes. How to kill an add event after a player leaves the arena? I tried to assign addevent to a variable and do stopevent on this variable, but it doesn't work either. please help
the only thing that can be said is:

Lua:
stopEvent(StayArena)
or
Lua:
stopEvent(StayArena[player:getId()])
 
I tried this way but it doesn't work. it's probably because the function called inside is getting stuck. I don't know how to solve it. There are situations where the player sometimes gets 2-3 rewards in every 15 seconds.
 
You have to save the event id returned by addEvent and use it for the stopEvent argument and find a way to call that whenever your player leaves with an onStepIn trigger or something
 
Untested but should work. Please let me know if you fail to understand any of it. I would of used registerEvent and unregisterEvent here, but considering I need to store the addEvent, may aswell just use our own table in memory....

It's pretty essential to make sure the arena is a non-logout zone!
Lua:
local events = {}

function rewardPlayer(playerId)
    local player = Player(playerId)
    if not player or not events[playerId] then
        if events[playerId] then
            events[playerId] = nil
        end
        return
    end
 
    --give rewards here

    events[playerId] = addEvent(rewardPlayer, 15000, playerId)
end

--Joining the arena
--Set a MoveEvent onStepIn, change to whatever you want as long as the event is set
local arenaEnter = MoveEvent()
function arenaEnter.onStepIn(player, item, position, fromPosition)
    if not player:isPlayer()
        return false
    end
  
    local playerId = player:getId()
    events[playerId] = addEvent(rewardPlayer, 15000, playerId)
    return true
end
arenaEnter:aid(12345) -- change to tile or tp actionid
arenaEnter:register()

--Leaving the arena
--Set a MoveEvent onStepIn, change to whatever you want as long as the addEvent is halted.
--Death will already be handled by the addEvent (Player object will be nil)
local arenaLeave = MoveEvent()
function arenaLeave.onStepIn(player, item, position, fromPosition)
    if not player:isPlayer()
        return false
    end
 
    local playerId = player:getId()
    stopEvent(events[playerId])
    events[playerId] = nil
    return true
end
arenaLeave:aid(12345) -- change to tile or tp actionid
arenaLeave:register()

re-edited the code...
 
Last edited:
Back
Top