Jump to content

Why is onPlayerWasted triggered twice?


Recommended Posts

I'm encountering an issue with my script where it prints 'playerName died' twice when a vehicle explodes. Interestingly, it works fine when I press enter or jump into the water; it only prints once. Any suggestions on how to handle explosions in this script? Also, any tips on optimizing the script for better efficiency would be appreciated.

function onPlayerWasted(totalAmmo, killer)
    local playerName = getPlayerName(source)
    
    if isElement(killer) and getElementType(killer) == "player" then
        local killerName = getPlayerName(killer)
        local victimName = getPlayerName(source)
        
        if killer == source then -- suicide
            outputChatBox(playerName .. " died")          
        else
			outputChatBox(killerName .. " killer " .. victimName)
        end
    else
        outputChatBox(playerName .. " died")
    end
end
addEventHandler("onPlayerWasted", root, onPlayerWasted)

 

Edited by Hiding
Link to comment
  • Hiding changed the title to Why is onPlayerWasted triggered twice?

Hello, could it be that the car explosion count as a player killing himself (suicide)? so then it print the suicide message and then the other one? maybe change the suicide message so they are not identical.

  • Like 1
Link to comment

Hi, try this

 

local lastDeathTime = {}

function onPlayerWasted(totalAmmo, killer)
    local playerName = getPlayerName(source)
    local currentTime = getTickCount()

    -- Check if the player has already been processed recently
    if lastDeathTime[playerName] and currentTime - lastDeathTime[playerName] < 1000 then
        return
    end

    lastDeathTime[playerName] = currentTime

    if isElement(killer) and getElementType(killer) == "player" then
        local killerName = getPlayerName(killer)
        local victimName = getPlayerName(source)

        if killer == source then -- suicide
            outputChatBox(playerName .. " died")
        else
            outputChatBox(killerName .. " killed " .. victimName)
        end
    else
        outputChatBox(playerName .. " died")
    end
end

addEventHandler("onPlayerWasted", root, onPlayerWasted)

 

  • Like 1
Link to comment
1 hour ago, mazarati21 said:

Hi, try this

 

local lastDeathTime = {}

function onPlayerWasted(totalAmmo, killer)
    local playerName = getPlayerName(source)
    local currentTime = getTickCount()

    -- Check if the player has already been processed recently
    if lastDeathTime[playerName] and currentTime - lastDeathTime[playerName] < 1000 then
        return
    end

    lastDeathTime[playerName] = currentTime

    if isElement(killer) and getElementType(killer) == "player" then
        local killerName = getPlayerName(killer)
        local victimName = getPlayerName(source)

        if killer == source then -- suicide
            outputChatBox(playerName .. " died")
        else
            outputChatBox(killerName .. " killed " .. victimName)
        end
    else
        outputChatBox(playerName .. " died")
    end
end

addEventHandler("onPlayerWasted", root, onPlayerWasted)

 

Thank you so much, it works now 😊

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...