• 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!

TFS 1.X+ onKill script get's triggered more than once

Raikou

Well-Known Member
Joined
Jul 18, 2007
Messages
145
Solutions
2
Reaction score
54
Hello all,

I got a problem with getting the onKill event triggered more than once.
Several checks have been done and my conclusion is that the script gets triggered several times.

The script is called within the creaturescript:
<event type="kill" name="Bosskill" script="Bosskill.lua"/>

From there on it lands into the following lua:

Lua:
function onKill(creature, target)
local config = {
    ["asmadeus"] = {
        loot = {{2152,100,85},{7759,25,5}},
        message = "Congratulations for defeating Asmadeus, Your reward is now in your backpack",
        BagId = 9774
    },

    local monster = config[Creature(target):getName():lower()]  --boss name to give reward
    local playerTable = {}
    if not monster  then --check if killed monster(target) is the boss
        print(Creature(target):getName():lower() .. " Not correct monster")
        return true --if not, return true
    end
    print(Creature(target):getName():lower() .. " Correct monster")
    for pid, _ in pairs(target:getDamageMap()) do --get damageMap from target killed
        local attackerPlayer = Player(pid) --get userData of each player that damaged the target   
        
        if attackerPlayer and playerTable[attackerPlayer] ~= 1 then --if player exist and not get reward yet
        local bag = attackerPlayer:addItem(monster.BagId, 1)       
            print("Created bag for: " .. attackerPlayer:getName() .. "!")
            for i = 1,#monster.loot do
                if monster.loot[i][2] >= math.random(1,100) then
                    local amount = math.random(1,monster.loot[i][3])

                        bag:addItem(monster.loot[i][1], amount, INDEX_WHEREEVER, FLAG_NOLIMIT)

                end                 
            end
            attackerPlayer:sendTextMessage(22, monster.message)
        end
        playerTable[attackerPlayer] = 1
        
    end
    return true
end

This boss is done by 2 to 4 people.
With 2 people it seems to be triggered mostly once, but if it 3 or 4 it gets triggered more often.
I made an attempt with the playerTable but it's only locally so that won't help if the script gets called twice.

Anyone has a good suggestion what's going on, or what i could change?
 
i had the same issue in the past, dont remember if i fixed or not but what i can tell is that attackerPlayer will trigger onkill for each member attacking the monster and players will reciving the loot multiple times. our best bet here is to work with onDeath.
 
i had the same issue in the past, dont remember if i fixed or not but what i can tell is that attackerPlayer will trigger onkill for each member attacking the monster and players will reciving the loot multiple times. our best bet here is to work with onDeath.
Mhmm, i`ll have a look at the onDeath version.

For now i somewhat solved it dirty with using a storagevalue that will be filled.

So the script still tries to give it out twice but the value in storagevalue blocks it.

Code:
asmadeus Correct monster
Created bag for: Tester!
Created bag for: Eerste!
asmadeus Correct monster
 
onKill is triggered for every player who was in combat with the creature. (there might be some limitations like within the past 60 seconds or something.. I haven't tested in awhile)

Anyway, it triggers for every player, and then you are grabbing every player a second time from the loop of the damageMap.

So with 3 people..

on Kill trigger for player 1
-> find all players in damageMap

on Kill trigger for player 2
-> find all players in damageMap

on Kill trigger for player 3
-> find all players in damageMap
 
I would personally suggest just using dropLoot, as an event callback, after loot is dropped, then get the corpse owner, which should be the person who gets the loot anyways, and either give it directly to them or in the corpse, and if you want the players to only receive reward once, storage value will probably be best way, no matter which event you use.
 
Back
Top