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 !
I read your code a few times and like you I’m puzzled, it shouled give the money to the player who died
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
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 ?
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.
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)
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.