• 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 How can I optimize this onDeath?

LucasFerraz

Systems Analyst
Joined
Jun 10, 2010
Messages
2,858
Reaction score
96
Location
Brazil
Hello guys,
How can I optimize it to the max?
Code:
function onDeath(cid, corpse, deathList)
    if getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).itemid == 2196 then
        if getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).aid > 1000 then
            doItemSetAttribute(getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).uid, "aid", 1)
        elseif getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).aid >= 2 then
            doPlayerRemoveItem(cid, getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).itemid, 1)
        else
            doItemSetAttribute(getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).uid, "aid", getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).aid+1)
        end
        doCreatureSetDropLoot(cid, false)
        return true
    elseif getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).itemid == 2173 then
        if getPlayerSkullType(cid) < 4 then
            return doCreatureSetDropLoot(cid, false)
        else
            return doCreatureSetDropLoot(cid, true)
        end
    else
        return true
    end
end
 
What exactly do you want to be different?
You can remove all the return (true) and else above return true and just add return true above the last end.
 
You mean like this? I just wanted the best optimization for that script below
Code:
function onDeath(cid, corpse, deathList)
    if getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).itemid == 2196 then -- RED SKULL AMULET
        if getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).aid > 1000 then
            doItemSetAttribute(getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).uid, "aid", 1)
        elseif getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).aid >= 2 then
            doPlayerRemoveItem(cid, getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).itemid, 1)
        else
            doItemSetAttribute(getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).uid, "aid", getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).aid+1)
        end
        doCreatureSetDropLoot(cid, false)
        return true
    elseif getPlayerSlotItem(cid, CONST_SLOT_NECKLACE).itemid == 2173 then -- AMULET OF LOSS
        if getPlayerSkullType(cid) < 4 then
            return doCreatureSetDropLoot(cid, false)
        end
    else
        return true
    end
end
 
No, only remove all return and return true and this else
Code:
     else
         return true
     end
end
Then add return true above the last end.
 
Code:
function onDeath(cid, corpse, deathList)
    if(not isPlayer(cid)) then
        return true
    end
  
    local targetItem = getPlayerSlotItem(cid, CONST_SLOT_NECKLACE)
    if targetItem.itemid == 2196 then -- RED SKULL AMULET
        if targetItem.aid > 1000 then
            doItemSetAttribute(targetItem.uid, "aid", 1)
        elseif targetItem.aid >= 2 then
            doPlayerRemoveItem(cid, targetItem.itemid, 1)
        else
            doItemSetAttribute(targetItem.uid, "aid", targetItem.aid + 1)
        end
      
        doCreatureSetDropLoot(cid, false)
        return true
    elseif targetItem.itemid == 2173 then -- AMULET OF LOSS
        if getPlayerSkullType(cid) < 4 then
            doCreatureSetDropLoot(cid, false)
        end
    end
    return true
end
 
An isPlayer if statement is not needed if the creaturescript is registered in login.lua (monsters don't login).
onDeath and onKill can be executed for monsters aswell, so id say yes.
Maybe you read some where that he added it into login.lua, I diden't.
 
If he registered it to monsters then he can just remove it from those monster files or scripts.
Creaturescripts (except from type login and logout) will only be executed for the creature where the event is registered.
Registering it in login.lua means it will be registered to all players and won't work for monsters.
For monsters it should be registered in the monster xml file or with an other script that registers the event to certain monsters.
 
Yes but as he diden't write anything for it I included it - "just in case".
If he has it registerd as a player event he can remove that if statment.
 
Back
Top