1

I have a FiveM script on the server side. The script fails to set xPlayer when I connect, when I reload the script once connected to the server everything works as expected.

RegisterServerEvent("eventScript:Ping", function()
     local _source = source
      Citizen.Wait(3000)
     print('Source: '.._source)
     local xPlayer = ESX.GetPlayerFromId(_source)
      print('xPlayer : '..ESX.GetPlayerFromId(_source))  
    if not gameInfo.PlayerTeams[_source] then
        gameInfo.PlayerTeams[_source] = {
            ["Team"] = xPlayer.job.name == 'army' and team2 or team1,
            ["Position"] = GetEntityCoords(GetPlayerPed(_source)),
            ["IsConnected"] = true
        } 
    end
end)
Nifim
  • 4,758
  • 2
  • 12
  • 31
  • any reason why you don't put any operations on xPlayer into a conditional statement if ESX.GetPlayerFromId(_source) may return nil? – Piglet Jan 14 '22 at 08:28
  • because _source is not supposed to be null since the request comes from the client And if my xPlayer is not defined, my script can't run – Magicien_ghost Jan 14 '22 at 17:08

1 Answers1

0

If you suspect the player hasn't properly loaded or hasn't been assigned all the identifiers, you need to wait until the playerJoining event is triggered to access that client's properties on the server. This is not specific to ESX.

abhi
  • 11
  • 1
  • 1