• 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 onkill multiple lootbag bug

Nokturno

Not a human
Joined
Aug 7, 2009
Messages
475
Solutions
1
Reaction score
347
hello. im currently using this piece of code wich i edited a bit to add lootbags to all party members that hit the boss
but the players are getting multiple lootboxes based on the amount of party members instead of just 1 per player


Lua:
local function getKillers(creature, party)
    local killers = {}
    local timeNow = os.mtime()
    local inFightTicks = configManager.getNumber(configKeys.PZ_LOCKED)
    for uid, cb in pairs(creature:getDamageMap()) do
        local attacker = Player(uid)
        if (attacker and attacker ~= creature and timeNow - cb.ticks <= inFightTicks) then
            local p = attacker:getParty()
            if p and p == party then
                killers[#killers +1] = attacker
            end
        end
    end
    return killers
end


function onKill(cid, target, damage, flags,war, player)
 local player = Creature(cid)
  local playerPos = player:getPosition()
  local playerDir = player:getDirection()
 
 
local killers = getKillers(target, player:getParty())
      for k, member in pairs(killers) do
    
    if isMonster(target) then
        local monster = config[getCreatureName(target)]
        if monster then
            local bag = doCreateItemEx(monster.BagId, 1)
            for i = 1,#monster.loot do
                if monster.loot[i][2] >= math.random(1,100) then -- random chance to get the item
                    local item = doAddContainerItem(bag, monster.loot[i][1],monster.loot[i][3])
                    if (monster.use_stats) then
                        for z = 1,#monster.chance_attr do
                            if(monster.chance_attr[z][2] >= math.random(1,100) and ItemInfo(monster.chance_attr[z][4], monster.loot[i][1]))then
                                doItemSetAttribute(item, monster.chance_attr[z][1], math.random(monster.chance_attr[z][3][1],monster.chance_attr[z][3][2])) -- add attribute
                            end
                        end
                    end
                end
            end
        
            if(getContainerItem(bag, 0).uid > 0)then -- check if the container has items or not
               -- doSendMagicEffect(getThingPos(cid), monster.effect)
               -- doSendAnimatedText(getThingPos(cid), monster.animatedText[2],monster.animatedText[1])
              
               member:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Congratulations!, you have cleared a distortion. your reward will be transfered into your bag.")
               Game.sendAnimatedText('Distortion Completed!', member:getPosition(), TEXTCOLOR_ELECTRICPURPLE)
            
                if monster.SendToDepot then
                    doPlayerSendMailByName(getCreatureName(cid), bag, getPlayerTown(cid), "Thais") -- send to depot
                else
                    doPlayerAddItemEx(member, bag,true) -- send to bag
                end
                doPlayerSendTextMessage(member, 25, monster.message)
            else
                doPlayerSendTextMessage(member, 25, "Better Luck Next Time,You Got No Reward.")
            end
        end
        end
    end
    

    return true
end

can someone help me here?
 

Kooda

Member
Joined
May 7, 2021
Messages
20
Solutions
4
Reaction score
12
Correct me if I'm wrong, but I think onKill triggers twice (in case lastHit is dif from mostDamage), I suggest changing the method to onDeath

Lua:
local function getKillers(creature, party)
    local killers = {}
    local timeNow = os.mtime()
    local inFightTicks = configManager.getNumber(configKeys.PZ_LOCKED)
    for uid, cb in pairs(creature:getDamageMap()) do
        local attacker = Player(uid)
        if (attacker and attacker ~= creature and timeNow - cb.ticks <= inFightTicks) then
            local p = attacker:getParty()
            if p and p == party then
                killers[#killers +1] = attacker
            end
        end
    end
    return killers
end


function onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
    local player = lasthitkiller and lasthitkiller or mostdamagekiller
    if not player:isPlayer() then
        player = player:getMaster()
    end
    if player and player:isPlayer() and isMonster(creature) then
        local killers = getKillers(creature, player:getParty())
        local monster = config[getCreatureName(creature)]
        if monster then
            for k, member in pairs(killers) do
                local bag = doCreateItemEx(monster.BagId, 1)
                for i = 1,#monster.loot do
                    if monster.loot[i][2] >= math.random(1,100) then -- random chance to get the item
                        local item = doAddContainerItem(bag, monster.loot[i][1],monster.loot[i][3])
                        if (monster.use_stats) then
                            for z = 1,#monster.chance_attr do
                                if(monster.chance_attr[z][2] >= math.random(1,100) and ItemInfo(monster.chance_attr[z][4], monster.loot[i][1]))then
                                    doItemSetAttribute(item, monster.chance_attr[z][1], math.random(monster.chance_attr[z][3][1],monster.chance_attr[z][3][2])) -- add attribute
                                end
                            end
                        end
                    end
                end
        
                if(getContainerItem(bag, 0).uid > 0)then -- check if the container has items or not
                   -- doSendMagicEffect(getThingPos(cid), monster.effect)
                   -- doSendAnimatedText(getThingPos(cid), monster.animatedText[2],monster.animatedText[1])
                  
                   member:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Congratulations!, you have cleared a distortion. your reward will be transfered into your bag.")
                   Game.sendAnimatedText('Distortion Completed!', member:getPosition(), TEXTCOLOR_ELECTRICPURPLE)
                
                    if monster.SendToDepot then
                        doPlayerSendMailByName(getCreatureName(cid), bag, getPlayerTown(cid), "Thais") -- send to depot
                    else
                        doPlayerAddItemEx(member, bag,true) -- send to bag
                    end
                    doPlayerSendTextMessage(member, 25, monster.message)
                else
                    doPlayerSendTextMessage(member, 25, "Better Luck Next Time,You Got No Reward.")
                end
            end
        end
    end
    return true
end

Made some tweaks as well for optimization

Try now registering the creature event FOR THE MONSTER and don't register it for players.
 
Top