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!