• 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 Movements script with Event

Warmix

New Member
Joined
Mar 28, 2010
Messages
18
Solutions
1
Reaction score
2
Hello,
I have something like this:
Lua:
local runningEvents = {}

local function gainHpAndMana(cid)
    local player = Player(cid)
    if player then
        player:addMana(100)
        player:addHealth(150)
        runningEvents[1] = addEvent(gainHpAndMana, 2000, player:getId())
    end
    return true;
end


function onEquip(player, item, slot)
    if player:getStorageValue(40005) == -1 or player:getStorageValue(40005) == 0 then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Equip")
        player:setStorageValue(40005, 1)
        addEvent(gainHpAndMana, 2000, player:getId())   
    end
    return true
end

function onDeEquip(player, item, slot)
        stopEvent(runningEvents[1])
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Dequip")
    player:setStorageValue(40005, 0)
        return true
end


When you equip item you gain mana and hp per 2s, but I found bug there.
If I dequip and equip this item fast, it looks like double script runs :/ "Dequip" and "equip" is correct order, but you gain example per 1 sec. Why is that?
 
Not tested !
Lua:
local runningEvents = {}
local function gainHpAndMana(cid)
    local player = Player(cid)
    if not player then
        return
    end

    player:addMana(100)
    player:addHealth(150)
    runningEvents[1] = addEvent(gainHpAndMana, 2000, player:getId())
    return true
end
function onEquip(player, item, slot)
    local event = runningEvents[1]
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Equip")
    if not event then
        event = addEvent(gainHpAndMana, 2000, player:getId())
    end
    return true
end
function onDeEquip(player, item, slot)
    local event = runningEvents[1]
    stopEvent(runningEvents[1])
    if event then
        event = nil
    end
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Dequip")
    return true
end
 
hmm, so events can be reach not only from script that exectutes them? oh, thanks, it's working :)
and thanks for remove global storage :)
 
actually, not working :/ sometimes works, sometimes not :/ When you equip item and logout and login, doesn't gain hp and mana. Is this any idea how to repair this without using global storage?
 
actually, not working :/ sometimes works, sometimes not :/ When you equip item and logout and login, doesn't gain hp and mana. Is this any idea how to repair this without using global storage?
What if you update the script alittle?
Lua:
local runningEvents = {}
local function gainHpAndMana(cid)
    local player = Player(cid)
    if not player then
        return
    end
    player:addMana(100)
    player:addHealth(150)
    runningEvents[cid] = addEvent(gainHpAndMana, 2000, player:getId())
    return true
end
function onEquip(player, item, slot)
    local event = runningEvents[player:getId()]
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Equip")
    if not event then
        event = addEvent(gainHpAndMana, 2000, player:getId())
    end
    return true
end
function onDeEquip(player, item, slot)
    local event = runningEvents[player:getId()]
    stopEvent(runningEvents[player:getId()])
    if event then
        event = nil
    end
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Dequip")
    return true
end

Even tho im sure it wont solve all the problems it will atleast store the event for each player that equip the item.
I mean if one player put the item on and then an other player put the same regain item on it would only reg hp and mana for the player that did equip the item last.
Due to local event = runningEvents[1] so if we use cid instead of hardcoded value it will put an gainHpAndMana() loop for each player instead of trying to use the same space for all players and would end up only working for 1 player
You could also make an on stepOut/stepIn event to make sure the item kick back in to the regain loop if the player did logout/login.

edit:
Or make the local runningEvents = {} an "public" table and make the check each time the player login
 
Last edited:
Back
Top