Stellar AI

Errors

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:

  • source was used inside a callback, thread or Wait() 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 GetPlayerFromId does 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

Related tools: ESX script generator · Fix a FiveM Lua error · all script generators