Get player ID for TriggerClientEvent

I know this question has been asked many times, I must be doing something else wrong because I can’t get it to work.

local PlayerId = PlayerId()

In my client.lua I trigger the server event

TriggerServerEvent("ss-script:checkcash", PlayerId)

In my server.lua I trigger the client event:

AddEventHandler('ss-script:checkcash', function(PlayerId)
        TriggerClientEvent('ss-script:progressbar', PlayerId)
end)

When I do -1 it works so it’s something with the ID
TriggerClientEvent('ss-script:progressbar', -1)

The client side function PlayerId() retrieves the client side player id. This is not the server side id. You would need to call GetPlayerServerId as well to get the player’s server id on client side.

Also when triggering a server event from a client, there is a “hidden” parameter called source that is the player’s server id.

All in all you can just do this:

-- client side
TriggerServerEvent("ss-script:checkcash")

-- server side
AddEventHandler('ss-script:checkcash', function()
    TriggerClientEvent('ss-script:progressbar', source)
end)
1 Like

client’s PlayerId() and the server playerID (source) are different. When you trigger an event from the client to the server (ss-script:checkcash) the event handler already knows the source that triggered it. You can to something like:

--lua. server-side.
AddEventHandler('ss-script:checkcash', function()
        local PlayerId = source --needs to be saved in a local variable, otherwise you risk to have source changed inside the event.
        TriggerClientEvent('ss-script:progressbar', PlayerId)
end)

If you want to get the server source from the client, you need to use GetPlayerServerId - FiveM Natives @ Cfx.re Docs (you can also use client-side GetPlayerFromServerId - FiveM Natives @ Cfx.re Docs to get the client-side PlayerId based on the server source id).

--lua. client-side.
local PlayerId = PlayerId()
TriggerServerEvent("ss-script:checkcash", GetPlayerServerId(PlayerId))
2 Likes