• 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 Problem with rewards

Aquan

New Member
Joined
Jan 27, 2021
Messages
7
Reaction score
1
Hello, why do players get double or triple reward after killing a monster. Can someone help me? Thanks in advance and regards

Code:
function onKill(creature, target)
    local monsterName = "Water" --boss name to give reward
    if target:getName() ~= monsterName then --check if killed monster(target) is the boss
        return true --if not, return true
    end
 
    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 then --if player exist
            attackerPlayer:addItem(1111, 1) --give reward
        attackerPlayer:addLevel(1) --give reward
        creature:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Gratz, you received 1 level and apple for kill this monster.")
        end
    end
    return true
end
 
Last edited:
Since you’re using onKill this will trigger several times depending on how many players are attacking the monster, try to use onDeath instead and register the event to the monster.
Or you could just remove the damageMap loop and use creature instead I guess since the creature argument already is the same as the attackerPlayer you are creating.
 
It probably happens because the script is giving rewards to each player who dealt damage to the monster, without taking into account whether the player has already received the reward or not, so players are receiving multiple rewards if they have dealt damage to the monster multiple times.

You can try to keep a record of the players who have already received the reward:
Lua:
function onKill(creature, target)
    local monsterName = "Water"
    if target:getName() ~= monsterName then
        return true
    end

    local rewardedPlayers = {}

    for pid, _ in pairs(target:getDamageMap()) do
        if not rewardedPlayers[pid] then
            local attackerPlayer = Player(pid)
            if attackerPlayer then
                attackerPlayer:addItem(1111, 1)
                attackerPlayer:addLevel(1)
                creature:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Gratz, you received 1 level and apple for killing this monster.")
                rewardedPlayers[pid] = true
            end
        end
    end
    return true
end
 
Back
Top