C# onPlayerKilled. Killer ID always returning -1

Hi all.
I’m trying to use baseevents:onPlayerKilled however the killerID seems to always return -1. I’m not sure if this is because both computers are on the same network or something else.

This is what I have on the client.

EventHandlers["baseevents:onPlayerKilled"] += new Action<int>((killerID) =>
{
    Debug.WriteLine($"Killed by {killerID}");
});

and this is on the server.

EventHandlers["baseevents:onPlayerKilled"] += new Action<Player, int>(OnPlayerKilled);

public void OnPlayerKilled([FromSource]Player source, int killerID)
{
    Debug.WriteLine($"{source.Name} Killd by {Players[killerID].Name}");
}

Does anyone know what’s going wrong? I did find another post about this here but it seems to be fine for them. Maybe a recent update broke it or have I missed something?

Thanks for any help.

Having the exact same issue

Literally just figured this out. At the bottom of deathevents.lua, there is a loop that isn’t used anymore with onesync.

function GetPlayerByEntityID(id)
	for i=0,32 do
		if(NetworkIsPlayerActive(i) and GetPlayerPed(i) == id) then return i end
	end
	return nil
end

For testing I ended up replacing the 32 with an arbitrary number, but what you really should do is something like

function GetPlayerByEntityID(id)
	for i=0,#GetActivePlayers do
		if(NetworkIsPlayerActive(i) and GetPlayerPed(i) == id) then return i end
	end
	return nil
end

Edit:
I changed this to for i, player in ipairs(GetActivePlayers()) do but it starts returning -1 again so not sure what to do other than hard code an arbitrary number

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.