[HELP] Get PlayerId from Ped?


I’m trying to make actions to some players by getting their PedId.
From the PedId, I need to retreive their “Server PlayerId”

After searching on this forum for a solution, and testing on my server, I’m a very confused so I need some clarifications on some points :

  • Is the source var the real ID of the player in :
-- Client Side 

-- Server Side
AddEventHandler('test', function()
	print(source) -- ?
  • What is the difference between this source var, the result from GetPlayerFromServerId function and the result from GetPlayerIndex() ?

  • How can I get the PlayerId from the PedId on this simple exemple ?

-- Simplified testing case :
-- I get the PlayerPed of the player and try to get back it's ID from this PlayerPed
local ped = GetPlayerPed(GetPlayerFromServerId(source))
-- Same as local ped = GetPlayerPed(-1) ?
Citizen.Trace("Player Ped : " .. ped) -- Gives me 1532 for exemple
local target = Citizen.InvokeNative("NETWORK_GET_PLAYER_INDEX_FROM_PED", ped)
Citizen.Trace("Target = " .. target .. " VS PlayerID : " .. GetPlayerIndex()) -- Result : 1532 VS -1 ???

@Dante_Kane So this is the… “target” thing ?

@AlfredLamoule As far as I know, some servers used workarounds to detect the target Player, but the global thing is the target Player has to make an action (command, pressed key, etc…) in order to be identified.

So this means that the NETWORK_GET_PLAYER_INDEX_FROM_PED native does not work at all on FiveM ?

I’m aware that on some servers (from what I saw on stream or videos), there is a limitation where two players must trigger something to the server before interacting with each other… But I find this so much ridiculous…

Anyway, I found a workaround (it will do for the moment) : I save the PedID on server side of each players at each spawn
EDIT : Annnnd… It won’t work because the PedId is not shared and not the same from a client to another…

Yeah this features remains a challenge so far. Sad

Any news for this function ?
Sorry for this necro post, but I trying to using it, and it seems its still quite messy. 2 Clients, for one, it’s working for the other, no :confused:

Found a workaround :

       -- ped is the 'target'
        for i = 0, 31 do
            if(ped == GetPlayerPed(i)) then
                  -- Do Stuff here


Eek…don’t get me wrong, I know it’s probably the only possible workaround. You likely have no other choice, unless the FiveM team comes up with a better solution.

But just the logic of doing it that way is so kludgey…I don’t know if my brain would let me write such inefficient code, lol

1 Like

The function works fine though?

local localIdFromPed = NetworkGetPlayerIndexFromPed(PlayerPedId())
local localId = PlayerId()

-- note how both will be equivalent!

That’s why I said ‘workaround’.
Loops aren’t the best ways, but when it’s the only way to perform comparison …

The function NetworkGetPlayerIndexFromPed is really full random.
On some client, it will work (return the playerID related to the ped), on some other, it will return 0.
Restarting client (or server) will change the ‘random’.

Cleaning cache (on both server & client) doesn’t help.

We can’t get the PlayerID of a ped (playerped) without iterating over all the PlayerID, and checking which ped is associated to who.

no, it’s not, it works perfectly fine. are you sure you passed a valid player ped at all, and not some other ped? also, 0 is a valid player ID (note how player IDs aren’t server IDs! use GetPlayerServerId to get a server ID), it returns -1 if it’s not a player