darkmu
Well-Known Member
I'm a little rusty, can anyone help me improve this code? What is happening is that if the second onwards kills the monster he is inserting it several times in the table, and I need to block to insert only once.
Lua:
function killMonsterHunter.onKill(creature, target)
local monster = Monster(target)
if not monster:isMonster() or monster:getMaster() or monster:isPlayer() then
return true
end
for pid, p in pairs(monster:getDamageMap()) do
local creature = Creature(pid)
if creature then
local player = Player(creature)
if player then
local position = target:getPosition()
if target:getName():lower() == 'rat' then
if #MonsterHunter > 0 then
for i = 1, #MonsterHunter do
if MonsterHunter[i].id ~= player:getGuid() then
local tableProperty = {
id = player:getGuid(),
name = player:getName(),
kills = 0
}
table.insert(MonsterHunter, tableProperty)
else
local killerTeam = MonsterHunter[i]
killerTeam.kills = killerTeam.kills + 1
end
end
else
local tableProperty = {
id = player:getGuid(),
name = player:getName(),
kills = 0
}
table.insert(MonsterHunter, tableProperty)
end
Game.createMonster('Rat', position, true, true)
end
end
end
end
return true
end