Get money for kill players

Hi! I’m looking for a way for players to earn money when they kill another player. I tried this way but, the money is given to the player who dies and not to the one who killed. Could someone please help me with this?

Citizen.CreateThread(function()
    alreadyDead = false
    while true do
        Citizen.Wait(50)
        local playerPed = GetPlayerPed(-1)
        if IsEntityDead(playerPed) and not alreadyDead 
         then
            killer = GetPedSourceOfDeath(playerPed)
            for id = 0, 255 do
                if killer == GetPlayerPed(id)
                 then 
                    killername = GetPlayerName(id)
                    (EVENT TO GIVE MONEY)
                end
            end
            alreadyDead = true
        end
        if not IsEntityDead(playerPed) then
            alreadyDead = false
    end
end
end)

Hey :slightly_smiling_face: !
I read your code a few times and like you I’m puzzled, it shouled give the money to the player who died :confused:
That makes me wonder, the error might be in the [EventToGiveMoney] code. As it is where the instructions directly giving the money are and the code above seems to be clean. If you can show me the Event’s code, I’d like that :slight_smile:

A thing that I noted though, you do a loop to get the ID of the killer, but when you call GetPedSourceOfDeath() it returns the entity ID and you should be able to send this ID to you event and use directly if i’m not mistaking no ?


I was thinking (not what you were looking for but wanted to mention it in case), their is the baseevents [onPlayerKilled] that are nice too, to do something like this.
https://docs.fivem.net/docs/resources/baseevents/events/onPlayerKilled/

Sorry if it wasn’t very usefull ^^’

There are easier ways to trigger code when someone dies. One way is to use the events provided by the “baseevents” resource. You can find them on the docs.

Another way (that doesn’t involve while-loops) is to use the “game events”. I wrote a post about them here.

Hey, thank you very much for your reply!

Like you, I’m quite puzzled to see that the money/xp is given to the player who dies…

Honestly what I was looking for is to give experience to the player who kills, and to do that I use this:

“TriggerEvent(“esx_xp:Add”,20)”

I don’t know why it doesn’t work tbh…

AddEventHandler('baseevents:onPlayerKilled', function(args)
    if eventName == 'CEventNetworkEntityDamage' then
        local victim = args[1]
        local culprit = args[2]
        local isDead = args[4] == 1

        if isDead and culprit == PlayerPedId() then
            if NetworkGetPlayerIndexFromPed(victim) ~= -1 then
                TriggerClientEvent('esx_xp:Add', killer, 1)
            end
        end
    end
end)

Running this line TriggerEvent("esx_xp:Add", 20) will ofc, adds to that client, in this case the client who died.

In this case, you should send the event to server and from the server you use TriggerClientEvent("esx_xp:Add", [killer_source], 20)

Hey, thank you very much for your reply!

I actually tried many ways, I’m sending you some of the ones I tried:

AddEventHandler(“baseevents:onPlayerKilled”, function(killer, reason)

local xPlayer = ESX.GetPlayerFromId(source)

local xp = math.random(5, 25)

local price = math.random(1000, 3000)

xPlayer.addAccountMoney(‘bank’, price)

TriggerEvent(‘esx_xp:addXP’, xPlayer.source , xp)

end)

onPlayerKilled runs client-side on the dead client, so all your code will be client-side here.
killer is the server id of the Killer.

Honestly, I have no clue how ESX works, but I’m almost sure that event like esx_xp:addXP are not client-side (and if they are, then the gamemode is bad).

Also, make sure that the baseevents resource is running on the server.

Yep indeed I checked, it runs the 'esx_xp' event runs server side :
[esx_xp/server/main.lua]
cfx2

This is server side code right ?
Try this :

AddEventHandler(“baseevents:onPlayerKilled”, function(killer, reason)

    local xPlayer = ESX.GetPlayerFromId(killer)

    local xp = math.random(5, 25)

    local price = math.random(1000, 3000)

    xPlayer.addAccountMoney(‘bank’, price)

    TriggerEvent(‘esx_xp:addXP’, killer , xp)

end)

This topic was automatically closed after 2 days. New replies are no longer allowed.