• 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 [1.2] Script executed 3 times? why?

Jacobs

Webdev, mapper
Joined
Jun 22, 2014
Messages
418
Solutions
4
Reaction score
316
Location
Slovakia
GitHub
Jacobs63
Hey, kind of getting back to lua and I don't understand why is my script being executed 3 times.

It has nothing to do with the 3 Ifs as I've tested it with just one in the beginning.

It keeps saying Gladiator 3 times in a row as well as it counts 10% of max hp 3 times in a row resulting into final of 133% maximum HP, instead of wanted 110%.

When you equip a helmet and you have the rest of the set equipped, your health is supposed to increase by 10%.

DeEquip works well as it's based on previous amount of health without any calculations.

Code:
function onEquip(cid, item, slot)
local gM = getPlayerMaxMana(cid)
local gH = getCreatureMaxHealth(cid)
local newH = gH*1.1
local newM = gM*1.1
if getPlayerSlotItem(cid, CONST_SLOT_ARMOR).itemid == 12642 then -- +merc gl warplate
if getPlayerSlotItem(cid, CONST_SLOT_LEGS).itemid == 18405 then -- +merc gl wall
if getPlayerSlotItem(cid, CONST_SLOT_FEET).itemid == 12646 then -- +merc gl greaves
doPlayerSendTextMessage(cid,22,"Gladiator")
Player(cid):setMaxHealth(newH)
Player(cid):setMaxMana(newM)
end
end
end
return TRUE
end
 
In some reason onequip does activates 3 times.
I guess its because first it check can it be equipped then it equips and 3rd time it does something more, but every time it goes trough the function.

Out there is a Limos fix for that, what I used for long time, but I have forgotten what is was.

Now i have this:
Code:
local scriptExecuted = false

function onEquip(player, item, slot)
    if not scriptExecuted then
        scriptExecuted = true
        addEvent(function() scriptExecuted=false end, 2000)
        bindCondition(player:getId(), "body", -1, {mL = 1})
        player:setStorageValue(SV.spellBuff8, 1)
    end
return true
end

In theory you can put the addEvent timer to even lower value.
And if you are worried that equipping might not work, because too many player might equip same item at the same time, then turn the scriptExecuted boolean into table and store booleans as value where key is player id.
 
In some reason onequip does activates 3 times.
I guess its because first it check can it be equipped then it equips and 3rd time it does something more, but every time it goes trough the function.

Out there is a Limos fix for that, what I used for long time, but I have forgotten what is was.

Now i have this:
Code:
local scriptExecuted = false

function onEquip(player, item, slot)
    if not scriptExecuted then
        scriptExecuted = true
        addEvent(function() scriptExecuted=false end, 2000)
        bindCondition(player:getId(), "body", -1, {mL = 1})
        player:setStorageValue(SV.spellBuff8, 1)
    end
return true
end

In theory you can put the addEvent timer to even lower value.
And if you are worried that equipping might not work, because too many player might equip same item at the same time, then turn the scriptExecuted boolean into table and store booleans as value where key is player id.
I just want to say good luck in more complex languages :)
 
Hey, kind of getting back to lua and I don't understand why is my script being executed 3 times.

It has nothing to do with the 3 Ifs as I've tested it with just one in the beginning.

It keeps saying Gladiator 3 times in a row as well as it counts 10% of max hp 3 times in a row resulting into final of 133% maximum HP, instead of wanted 110%.

When you equip a helmet and you have the rest of the set equipped, your health is supposed to increase by 10%.

DeEquip works well as it's based on previous amount of health without any calculations.

Code:
function onEquip(cid, item, slot)
local gM = getPlayerMaxMana(cid)
local gH = getCreatureMaxHealth(cid)
local newH = gH*1.1
local newM = gM*1.1
if getPlayerSlotItem(cid, CONST_SLOT_ARMOR).itemid == 12642 then -- +merc gl warplate
if getPlayerSlotItem(cid, CONST_SLOT_LEGS).itemid == 18405 then -- +merc gl wall
if getPlayerSlotItem(cid, CONST_SLOT_FEET).itemid == 12646 then -- +merc gl greaves
doPlayerSendTextMessage(cid,22,"Gladiator")
Player(cid):setMaxHealth(newH)
Player(cid):setMaxMana(newM)
end
end
end
return TRUE
end
Code:
    local equipment = {
        ['armor'] = 12642,
        ['legs'] = 18405,
        ['feet'] = 12646
    }
    local multiplier = 1.1
    local say = "Gladiator"
    function onEquip(cid, item, slot)
        local slots = getPlayerSlotItem(cid, slot)
        if slots.itemid ~= item.itemid then
            return true
        end
        local player = Player(cid)
        local maxMana = getPlayerMaxMana(cid)
        local maxHp = getCreatureMaxHealth(cid)
        local newHp = maxHp * multiplier
        local newMana = maxMana * multiplier
        if getPlayerSlotItem(cid, CONST_SLOT_ARMOR).itemid == equipment['armor'] then -- +merc gl warplate
            if getPlayerSlotItem(cid, CONST_SLOT_LEGS).itemid == equipment['legs'] then -- +merc gl wall
                if getPlayerSlotItem(cid, CONST_SLOT_FEET).itemid == equipment['feet'] then -- +merc gl greaves
                    doPlayerSendTextMessage(cid, 22, say)
                    player:setMaxHealth(newHp)
                    player:setMaxMana(newMana)
                end
            end
        end
    return true
    end

source of the fix : https://otland.net/threads/tfs-1-0-movement-loops-3-times.209686/#post-2009551
 
Here is the updated version sent via pm for those wanting the same thing
Code:
    local equipment = {
        { itemid = 12642, slot = CONST_SLOT_ARMOR },
        { itemid = 18405, slot = CONST_SLOT_LEGS },
        { itemid = 12646, slot = CONST_SLOT_FEET }
    }
  
    local multiplier = 1.1
    local say = "Gladiator"
  
    local count = 0
  
    function onEquip(cid, item, slot)
        local slots = getPlayerSlotItem(cid, slot)
        if slots.itemid ~= item.itemid then
            return true
        end
        local player = Player(cid)
        local maxMana = getPlayerMaxMana(cid)
        local maxHp = getCreatureMaxHealth(cid)
        local newHp = maxHp * multiplier
        local newMana = maxMana * multiplier
        for _, equiped in pairs(equipment) do
            if getPlayerSlotItem(cid, equiped.slot).itemid == equiped.itemid then
                count = count + 1
            end
        end
        if count == #equipment then      
            doPlayerSendTextMessage(cid, 22, say)
            player:setMaxHealth(newHp)
            player:setMaxMana(newMana)
        end
        return true
    end
 
OnEquip works only once, after that it doesnt do anything.
onDeEquip works well, since the dequipped message is being shown.

Code:
    local equipment = {
        { itemid = 12642, slot = CONST_SLOT_ARMOR },
        { itemid = 18405, slot = CONST_SLOT_LEGS },
        { itemid = 12646, slot = CONST_SLOT_FEET }
    }
 
    local multiplier = 1.1
    local say = "Gladiator"
 
    local count = 0
 
    function onEquip(cid, item, slot)
        local slots = getPlayerSlotItem(cid, slot)
        if slots.itemid ~= item.itemid then
            return true
        end
        local player = Player(cid)   
        local level = getPlayerLevel(cid)
        local maxMana = 35+5*(level-8)
        local maxHp = 185+15*(level-8)   
        local newHp = maxHp * multiplier
        local newMana = maxMana * multiplier
        for _, equiped in pairs(equipment) do
            if getPlayerSlotItem(cid, equiped.slot).itemid == equiped.itemid then
                count = count + 1
            end
        end
        if count == #equipment then     
            doPlayerSendTextMessage(cid, 22, say)
            player:setMaxHealth(newHp)
            player:setMaxMana(newMana)
        end
        return true
    end

function onDeEquip(cid, item, slot)
    local player1 = Player(cid)   
    local level1 = getPlayerLevel(cid)
    local maxMana1 = 35+5*(level1-8)
    local maxHp1 = 185+15*(level1-8)       
                doPlayerSendTextMessage(cid,22,"dequipped")
                player1:setMaxHealth(maxHp1)
                player1:setMaxMana(maxMana1)
return true
end
 
Back
Top