• 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 cancel a started event?

Sparkles

snek
Joined
Mar 1, 2009
Messages
320
Solutions
27
Reaction score
148
Location
Norway, Tromsø
I think I might be blind, but eh.
Im trying to kick players from the Demon Oak area after x amount of seconds, so they dont block it for others.
Somehow, using the normal axe on the tree after entering doesn't stop the event, can someone please tell me what im doing wrong here?

lua:

Lua:
 function onUse(player, item, fromPosition, target, toPosition, isHotkey)
        local specs, spec = Game.getSpectators(DEMON_OAK_POSITION, false, false, 9, 9, 6, 6)
        for i = 1, #specs do
            spec = specs[i]
            if player:getStorageValue(Storage.DemonOak.Progress) < 1 or player:getStorageValue(Storage.DemonOak.Progress) == 2 then 
                if spec:isPlayer() then
                    player:sendTextMessage(MESSAGE_STATUS_SMALL, "Someone is already in there!")
                return true
                end
            end
            if spec:isMonster() then
            spec:remove()
            end
        end
        
      
local function kickbois(cid)
    local player = Player(cid)
    if player then
        player:teleportTo(DEMON_OAK_KICK_POSITION)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You should discuss your findings with Oldrak.')
    end
end


            
        
        if target.actionid == 50250 and target.itemid == 2709 and player:getStorageValue(Storage.DemonOak.Progress) < 1 then
            if player:getLevel(cid) >= 120 then
                player:teleportTo(DEMON_OAK_ENTER_POSITION)
                addEvent(kickbois, 20000, player.uid)
                player:setStorageValue(Storage.DemonOak.Progress, 1)
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'It seems that you have discovered an Ultimate Challenge.')
            else
                player:sendTextMessage(MESSAGE_STATUS_SMALL, 'You need to advance to level 120 before going into this place.')
            end 
        elseif target.actionid == 50250 and target.itemid == 2709 and player:getStorageValue(Storage.DemonOak.Progress) == 1 then
            player:teleportTo(DEMON_OAK_KICK_POSITION)
            stopEvent(kickbois)
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You should discuss your findings with Oldrak.')
            
        end

end
 
Solution
You are setting kickOutEvent to nil every time you use an item. Move kickOutEvent out of that function and try to remove local before it. So something like that:
Lua:
kickOutEvent = nil

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

    if player then
        player:teleportTo(DEMON_OAK_KICK_POSITION)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You should discuss your findings with Oldrak.')
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local specs, spec = Game.getSpectators(DEMON_OAK_POSITION, false, false, 9, 9, 6, 6)

    for i = 1, #specs do
        spec = specs[i]

        if player:getStorageValue(Storage.DemonOak.Progress) < 1 or...
This script does indeed work as intended, thanks for the help. But this is a local event when using this item only(?) I need to put this in two separate scripts and tried copy pasting but then I get this error:
Code:
table index is nil
stack traceback:
[C]: in function '__newindex'
at this line
Lua:
kickOutEvent[cid] = addEvent(kickPlayerOut, 60000, cid)
And yet again, I cant figure out why. wtf

I'd think that when I added this:
Lua:
local kickOutEvent = {}

local function kickPlayerOut(cid)
    local player = Player(cid)
    if player then
        player:teleportTo(DEMON_OAK_KICK_POSITION)
        player:setStorageValue(Storage.DemonOak.Progress, 2)
        player:setStorageValue(cStorage, 0)
        player:setStorageValue(progress, 0)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You took too long to finish the quest!')
    end
end
That it would define kickOutEvent as the registered event of kickPlayerOut and not return a nil value?

wadu heck
You should be able to duplicate it without problems. As long as you have cid defined and the array defined.
local cid = player.uid
local kickOutEvent = {}

If you still have issues you'll have to post the new script to get help fixing it.
 
This script does indeed work as intended, thanks for the help. But this is a local event when using this item only(?) I need to put this in two separate scripts and tried copy pasting but then I get this error:
Code:
table index is nil
stack traceback:
[C]: in function '__newindex'
at this line
Lua:
kickOutEvent[cid] = addEvent(kickPlayerOut, 60000, cid)
And yet again, I cant figure out why. wtf

I'd think that when I added this:
Lua:
local kickOutEvent = {}

local function kickPlayerOut(cid)
    local player = Player(cid)
    if player then
        player:teleportTo(DEMON_OAK_KICK_POSITION)
        player:setStorageValue(Storage.DemonOak.Progress, 2)
        player:setStorageValue(cStorage, 0)
        player:setStorageValue(progress, 0)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You took too long to finish the quest!')
    end
end
That it would define kickOutEvent as the registered event of kickPlayerOut and not return a nil value?

wadu heck

It is saying that "kickOutEvent" is not set up.
The issue is in here "kickOutEvent[cid]", because "kickOutEvent" is nil (not a table as your intention) at the issue line.
You are probably creating the event outside this script, or at least before the line of "local kickOutEvent = {}".
You cannot read "kickOutEvent[cid]" before "local kickOutEvent = {}", not even outside the scope of "kickOutEvent" (where you are able to read this value). Because on both cases, you cannot see the kickOutEvent, either because it does not exists or you have not access for it (because of scope).

kickOutEvent should not be a local variable if your intention is to use it in multiple scripts.

Therefore, if you declare "kickOutEvent" as global at a simple script (like action, movement and so on), you will need to trust on the loading order (which is a bad way). This means you will need to trust the this script should load before where you register the event.
To solve that, you need to declare "kickOutEvent" in the constant file.
It will ensure to load before any script and, if you don't use the "local" keyword, it will be a global value where you can use anywhere that is ready to use.

In resume:
Declare "kickOutEvent" as a global variable (without "local" keyword) at constants lib file: kickOutEvent = {}
Don't forget to remove the line of "local kickOutEvent = {}" at your actual script file.

In addition, a good way to code is to clean everything that is not in use anymore.
It means that, after the event is finished, you should to remove it from the kickOutEvent.
Like that:
Lua:
local function kickPlayerOut(cid)
    local player = Player(cid)
    if player then
        player:teleportTo(DEMON_OAK_KICK_POSITION)
        player:setStorageValue(Storage.DemonOak.Progress, 2)
        player:setStorageValue(cStorage, 0)
        player:setStorageValue(progress, 0)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You took too long to finish the quest!')
    end
    kickOutEvent[cid] = nil -- Clear from event list after load the event
end

Another thing to mention is that (will change nothing on the code, but is good to learn), as you need to declare kickOutEvent as a global variable, a good common readable way for it is to use the format like that:
EVENTID_KICKOUT = {}
There is 2 entities (EVENTID and KICKOUT), that's why only 1 underline separating them.
If you separate everything like EVENT_ID_KICK_OUT, it will be hard to understand what you are mentioning.
As EVENTID_KICKOUT, we know that the id is of the event and that this is something about to "kick out".
 
Back
Top