onPlayerKilled killerID always -1

So I’m trying to give a weapon to a player who kills another player using onPlayerKilled. The problem is that the killerID is always -1

This is the server script:

RegisterNetEvent("baseevents:onPlayerKilled")
AddEventHandler("baseevents:onPlayerKilled", function(killerID, deathData)
	local victim = source
    print("victim: "..tostring(victim).." | killer: "..tostring(killerID))
    TriggerClientEvent("test", killerID, victim)
end)

The message in the console:

victim: 8 | killer: -1

No matter how the killer kills and who kills.

If someone knows how to help me please reply, thanks!

You have to specify the killer as an id. In this snippet you are not specifying killer as anything. Try

PlayerPedId() for “killer”

When you are testing it how do you die ?
Through a Vehicle ?

No, this is a Baseevent.
See in Docs it should return what he wants.
I think he just is not getting killed by smth. other than some vehicles because that explains the result.

1 Like

Nope, I was testing it with other players with weapons not vehicles

did you fix this, the same problem is happening to me.

Yes, don’t use baseevents.

Instead i used CEventNetworkEntityDamage

AddEventHandler("gameEventTriggered", function(name, args)
	if not (name == "CEventNetworkEntityDamage") then return end

	-- Check if victim is dead
	if not (args[4] == 1) then return end

	local victimId = GetPlayerServerId(NetworkGetPlayerIndexFromPed(args[1]))
	local killerId = GetPlayerServerId(NetworkGetPlayerIndexFromPed(args[2]))
end)

This function will return if the victim is not dead. To get the victim and killer you need to use args[1] for the victim and args[2] for the killer. args[4] will return 1 or 0 depending if the attack is fatal or not.

For the full list of args you can check this image


found on github FiveM-Documentation/EventList.md at master · logan-mcgee/FiveM-Documentation · GitHub

if(args[4] == 1) never returns true for me?