ESX: 'attempt to index a nil value (local xPlayer)' – Fix
The error
SCRIPT ERROR: @esx_myjob/server/main.lua:20: attempt to index a nil value (local 'xPlayer')
What it means
ESX.GetPlayerFromId(source) returns nil when there is no loaded ESX player for that ID. Usual causes:
sourcewas used inside a callback, thread orWait()after it changed. Copy it to a local first.- The ID came from the client as an argument and is not a real server ID.
- The player disconnected, or the event fired before
esx:playerLoaded. - The code runs client-side, where
GetPlayerFromIddoes not exist.
The fix
RegisterNetEvent('esx_myjob:pay', function()
local src = source -- capture immediately
local xPlayer = ESX.GetPlayerFromId(src)
if not xPlayer then return end -- player not loaded or gone
xPlayer.addMoney(100)
end)Always nil-check xPlayer before using it, and never trust a player ID sent from the client for anything involving money or items.
Related errors
- qbcore getcoreobject nil
- fivem couldnt load resource fxmanifest
- fivem oxmysql no such export
- esx getsharedobject deprecated
Related tools: ESX script generator · Fix a FiveM Lua error · all script generators