• 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 stopEvent not working

Watchdog87

Member
Joined
Mar 2, 2014
Messages
30
Solutions
1
Reaction score
5
Hello!

I'm using OTHire 0.0.2.

I'm trying to create a movement script that gives players faster health and mana regeneration when they are sitting on benches.

The problem is that, when stepping away from the bench or logging out, it doesen't stop the Event, so the console gives an error that it can't find the player.

Code:
    local cfg = {
        health = 13, --change how much health the player recieves--
        mana = 13, --change how much mana the player recieves--
        timeInMilli = 1000 --change in how much time the player receives health and mana, 1000ms = 1 second--
        }  
      
    function onStepIn(cid, item, pos)
        local function ManaHealthRegen(parameter)
            doPlayerAddHealth(parameter.cid, cfg.health)
            doPlayerAddMana(parameter.cid, cfg.mana)
            addEvent(ManaHealthRegen, cfg.timeInMilli, {cid = parameter.cid})
            doPlayerSay(cid,"Your health and mana regeration has increased by 10%.",16)
            doSetCreatureLight(cid, 10, 11, 20000)
        return true  
        end  
            addEvent(ManaHealthRegen, cfg.timeInMilli, {cid = cid})
    return true      
    end
  
    function onStepOut(cid, item, pos)
            stopEvent(ManaHealthRegen)
    return true  
    end

And this is what I added in the movement.xml file:

Code:
    <!-- Bench Extra Regen -->
    <movevent event="StepIn"  itemid="1662" script="bench.lua" />
    <movevent event="StepOut" itemid="1662" script="bench.lua" />
    <movevent event="StepIn"  itemid="1663" script="bench.lua" />
    <movevent event="StepOut" itemid="1663" script="bench.lua" />  
    <movevent event="StepIn"  itemid="1664" script="bench.lua" />
    <movevent event="StepOut" itemid="1664" script="bench.lua" />
    <movevent event="StepIn"  itemid="1665" script="bench.lua" />
    <movevent event="StepOut" itemid="1665" script="bench.lua" />
 
Not tested:

Code:
local k = nil
local cfg = {
    health = 13, --change how much health the player recieves--
    mana = 13, --change how much mana the player recieves--
    timeInMilli = 1000 --change in how much time the player receives health and mana, 1000ms = 1 second--
}
 
function onStepIn(cid, item, topos, frompos)
    local function ManaHealthRegen()
        doPlayerAddHealth(cid, cfg.health)
        doPlayerAddMana(cid, cfg.mana)
        addEvent(ManaHealthRegen, cfg.timeInMilli)
        doPlayerSay(cid,"Your health and mana regeration has increased by 10%.",16)
        doSetCreatureLight(cid, 10, 11, 20000)
    end
    k = addEvent(ManaHealthRegen, cfg.timeInMilli)
    return true 
end

function onStepOut(cid, item, topos, frompos)
    if k ~= nil then
        stopEvent(k)
    end
end
 
Last edited:
Hmm, if you're intializing the event as a global variable (k), it's not going to work out very well with multiple players. You could try work with storages, or use this K-value as a list with cid's as keys.

Ignazio
 
Not tested:

Code:
local k = nil
local cfg = {
    health = 13, --change how much health the player recieves--
    mana = 13, --change how much mana the player recieves--
    timeInMilli = 1000 --change in how much time the player receives health and mana, 1000ms = 1 second--
}

function onStepIn(cid, item, topos, frompos)
    local function ManaHealthRegen()
        doPlayerAddHealth(cid, cfg.health)
        doPlayerAddMana(cid, cfg.mana)
        addEvent(ManaHealthRegen, cfg.timeInMilli)
        doPlayerSay(cid,"Your health and mana regeration has increased by 10%.",16)
        doSetCreatureLight(cid, 10, 11, 20000)
    end
    k = addEvent(ManaHealthRegen, cfg.timeInMilli)
    return true
end

function onStepOut(cid, item, topos, frompos)
    if k ~= nil then
        stopEvent(k)
    end
end

This one didn't work. The event never stops.
 
cid = creature:getId() -- creature is userdata..
event = {}
event[cid] = addEvent()
stopEvent[event[cid]]

Im using TFS 1.0 not sure will it work for you.

But what does this do is: registers event on with ID and whenever you want to stop it, just use the creature ID again.
If there is nothing to stop (event already has executed) no errors are thrown.
If you want more exact name for event, then change the event to more specific event name like: foodEvent or ["item1110Event"] or put the event in some specific table like
monster.event or tooCoolToBeTable.event
 
Back
Top