• 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 Question to lua people

OperatorMopa

Member
Joined
Feb 10, 2014
Messages
127
Reaction score
6
Can you explain to me what is happening here ?
What i wanna do is to make a table with all the players who dealt more then 10% dmg on player who died.
TFS 1.2

I made this creatureevent
Code:
local killConfig = {
    considerAttackers ={}
}
function onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
    if creature:isMonster() then
        return true
    end
    -- totalDMG counter
    local totalDMG = 0
    for id, damage in pairs(creature:getDamageMap()) do
        local player = Player(id)
        if player then
            totalDMG = totalDMG+ damage.total
        end
    end

    -- considerAttackers
    local considerAttackers = 1
    for id, damage in pairs(creature:getDamageMap()) do
        local player = Player(id)
        if player then
            print(player:getGuid(), damage.total / totalDMG )
            if damage.total / totalDMG >= 0.1 then
                killConfig.considerAttackers[considerAttackers] = {uid = player:getGuid(), dmgPercent = damage.total / totalDMG}
                considerAttackers = considerAttackers+1
            end
        end
    end

    print('====== considerAttackers ======')
    for i=1, #killConfig.considerAttackers do
        print(killConfig.considerAttackers[i].uid, killConfig.considerAttackers[i].dmgPercent)
    end
    return true
end

And this is the effect:
Code:
55      0.086868686868687
52      0.91313131313131
====== considerAttackers ======
52      0.91313131313131
52      0.67676767676768

Why player 52 has been added 2 times?
The weird part is that sometimes it works like charm :D
The reason i decided to make onDeath function is that onKill excecutes few times, when there are many attackers.

Maybe my idea is completely wrong and there is a better way to acheive it? Maybe c++ ?
 
Can you explain to me what is happening here ?
What i wanna do is to make a table with all the players who dealt more then 10% dmg on player who died.
TFS 1.2

I made this creatureevent
Code:
local killConfig = {
    considerAttackers ={}
}
function onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
    if creature:isMonster() then
        return true
    end
    -- totalDMG counter
    local totalDMG = 0
    for id, damage in pairs(creature:getDamageMap()) do
        local player = Player(id)
        if player then
            totalDMG = totalDMG+ damage.total
        end
    end

    -- considerAttackers
    local considerAttackers = 1
    for id, damage in pairs(creature:getDamageMap()) do
        local player = Player(id)
        if player then
            print(player:getGuid(), damage.total / totalDMG )
            if damage.total / totalDMG >= 0.1 then
                killConfig.considerAttackers[considerAttackers] = {uid = player:getGuid(), dmgPercent = damage.total / totalDMG}
                considerAttackers = considerAttackers+1
            end
        end
    end

    print('====== considerAttackers ======')
    for i=1, #killConfig.considerAttackers do
        print(killConfig.considerAttackers[i].uid, killConfig.considerAttackers[i].dmgPercent)
    end
    return true
end

And this is the effect:
Code:
55      0.086868686868687
52      0.91313131313131
====== considerAttackers ======
52      0.91313131313131
52      0.67676767676768

Why player 52 has been added 2 times?
The weird part is that sometimes it works like charm :D
The reason i decided to make onDeath function is that onKill excecutes few times, when there are many attackers.

Maybe my idea is completely wrong and there is a better way to acheive it? Maybe c++ ?
You are not reseting the table "considerAttackers" then its showing values from other runs.

You should put it inside the function onDeath.
 
Back
Top