[Clarify]Server/client communication delay and execution order.

Hi community. I am a complete newbie in FiveM scripting and developing in general. I’m curently experimenting with transfer of data between server/client and vice versa. In the following example I am trying to retrieve the local player id to the server and store it in a variable. I have the following code:

Server side:

local localPlayerId

RegisterCommand("source", function(source, args, rawCommand)
    print("source command triggered")
    local src = source
    local playerId = TriggerClientEvent('getLocalPlayerIdFromClient', src)
    print("Loading player id.")
    print(localPlayerId)

end, false)

RegisterNetEvent('sendPlayerId:server')
AddEventHandler('sendPlayerId:server', function(playerIdent)
    print("sendPlayerId:server even triggered " .. playerIdent)
    localPlayerId = playerIdent
end)

Client side:

local spawnPos = vector3(-275.522, 6635.835, 7.425)

AddEventHandler('onClientGameTypeStart', function()
    exports.spawnmanager:setAutoSpawnCallback(function()
        exports.spawnmanager:spawnPlayer({
            x = spawnPos.x,
            y = spawnPos.y,
            z = spawnPos.z,
            model = 'a_m_m_skater_01'
        }, function()
            TriggerEvent('chat:addMessage', {
                args = { 'Welcome to the party!~' }
            })
        end)
    end)

    exports.spawnmanager:setAutoSpawn(true)
    exports.spawnmanager:forceRespawn()
end)

RegisterNetEvent('getLocalPlayerIdFromClient')
AddEventHandler('getLocalPlayerIdFromClient', function()
    local player = PlayerId()
    print("getLocalPlayerIdFromClient event")
    TriggerServerEvent('sendPlayerId:server', player)
end)

Essentially what I am trying to do is register a command “/source” on the server, which when executed triggers a client event that stores the PlayerId() into a variable, which is then passed back to the server by triggering a server event and then storing this value into a server local variable, and then print the value from the server side. I get it that is probably the worst thing to do. I basically have no idea what I am doing right now, so I’m experimenting with stuff to get the hang of it. Now, everything works fine except the expected delay of execution, which kinda makes no sense to me and I would need a bit of clarification and tips.

I get the following result when I execute “/source” command from the client:

This is what I get in the chatbox:

And in the VScode terminal:

What is strange to me is that this line “print(“getLocalPlayerIdFromClient event”)” is executed before this one: “print(“source command triggered”)”. I expect the source command triger print to be the first in the execution order.

"print(“sendPlayerId:server even triggered " … playerIdent)” is not displaying in the game console, neither in chat, and only in the VScode terminal, yet “print(“source command triggered”)” is displaying in the game console and chat and not in the VScode terminal. Both are server sided executions of print.

And then the first time I execute the command from the client the server side var localPlayerId is nill, which makes sense because the invocation of print is faster than the communication between the server/client. After I execute the command a second time I am able to print the playerId of the local client from server side accuratelly. My question here is, how can I wait for the print in order to let the var to be stored before printing it? I tried using a thread but failed miserably:

Thanks in advance!

You are forgetting the fact that sending any data between server/client takes time (usually your ping).
Triggering a client event which then triggers a server event and expecting the data to be there immediately is not how it works.

You can wait til the data is there or use something like a callback system. These days many people use the one from ox_lib, I however have my own called kimi_callbacks. Both are free and can be found on github and this forum.
They allow you to “request” data from the server (or a client) and then just work with it. Either synchronously and inline or asynchronously with callback methods.

Another thing regarding player ids:
These are not shared. The client side player ids are not the same as the ones on server side. Usually the source is the server side player id of the executing player (triggered a command or event).
For getting other player server ids on client side, you take their client side player id and use GetPlayerServerId - FiveM Natives @ Cfx.re Docs to get their server id and use that one on the server.

Hopefully that clarified some of the issues you are having :slight_smile: