Guide

Attempt to index a nil value (local 'xPlayer'): ESX fix

Why ESX shows "attempt to index a nil value (local xPlayer)", how a player leaving causes it, and the nil check that stops the error.

SCRIPT ERROR: @resource_name/server/main.lua:LINE: attempt to index a nil value (local 'xPlayer')

The short answer: xPlayer is nil, which means ESX did not find a player for the id your script used. The script then calls a method on it, such as xPlayer.getInventoryItem, and Lua stops with this error. The usual reason is that the player left the server before the code ran, or the code ran before the player had loaded. The fix is a nil check right after ESX.GetPlayerFromId, plus a fresh lookup inside any delayed code.

What causes this error?

ESX gives each connected player an xPlayer object. You get it with ESX.GetPlayerFromId:

local xPlayer = ESX.GetPlayerFromId(source)

When no player matches that id, the function returns nil. The ESX docs say to check for nil before you use xPlayer. Scripts often skip that check, so the error only shows up when a player does something unusual, such as leaving during a timer.

The forum thread that covers this error most often describes this case. A player starts a harvest or sell action. The script schedules the result with SetTimeout. The player disconnects before the timer fires. When the timer runs, xPlayer is nil, and the next line that uses it fails.

Other ESX resources show the same message, including es_extended and some job or billing scripts. The line that fails is different in each one, but the cause is the same: a player lookup that returned nothing.

Last checked: 2 December 2026

Fix it step by step

  1. Read the full error in the server console. Note the resource name and the line number. The message tells you which file to open. For more on reading these messages, see how to debug FiveM scripts.
  2. Open that file and find the line that uses xPlayer.
  3. Scroll up to see where xPlayer was set. It should come from ESX.GetPlayerFromId.
  4. Add a nil check directly after that lookup:
local xPlayer = ESX.GetPlayerFromId(source)

if not xPlayer then
    return
end
  1. If the failing code runs inside SetTimeout or another callback, save the source in a local variable before the delay. Then look up the player again inside the callback and check it again:
RegisterNetEvent('myresource:harvest', function()
    local src = source
    local xPlayer = ESX.GetPlayerFromId(src)

    if not xPlayer then
        return
    end

    SetTimeout(5000, function()
        local player = ESX.GetPlayerFromId(src)

        if not player then
            return
        end

        player.removeInventoryItem('item', 1)
    end)
end)

Replace myresource:harvest, item, and the inventory call with the names your script uses. Check the method names against the ESX version on your server, because they have changed between versions.

  1. Repeat the same check in every server script that calls xPlayer. One fixed file does not fix the others.
  2. Restart the resource, or restart the server.

How do I check that the fix works?

  1. Join the server with a test account.
  2. Start the action that used to fail, such as the harvest or sell step.
  3. Disconnect during the timer, or close FiveM while the action is running.
  4. Watch the server console. The error should not appear, and the other players should see no crash.

If the error still appears, the line in the message is not the one you changed. Open the file named in the message again and check that line.

If it still fails

  • The error is inside a third-party resource you cannot edit. Check the author's support channel or the latest release. Do not edit the file without a backup.
  • The error comes from a callback that runs on join. The player may not be loaded yet. Move the code so it runs after the player has loaded, and keep the nil check.
  • You have an old ESX = nil setup with esx:getSharedObject. Some older scripts load ESX this way. Compare your loading code with the current ESX docs before you change the player lookup.
  • Other errors appear after you add the check. Read the new error. A nil check stops one failure, but it can expose a second bug that was hidden behind it.

Some forum members say this error does not always crash the server. One reply in the main thread says the script keeps working in most cases. The error is still a sign that a player was missing, so add the check anyway, because a missing check can break the rest of the action.

Frequently asked questions

What does "attempt to index a nil value (local 'xPlayer')" mean?

It means the code tried to use xPlayer, but xPlayer was nil. ESX returned no player for the id the script passed to ESX.GetPlayerFromId.

Why does this happen only sometimes?

It usually happens when a player leaves or lags during an action, or when a timer or callback runs after the player has gone. It does not happen on every join or every use of the feature.

Is adding a nil check enough?

It stops the error, but it may not fix the action. If a player leaves during a harvest, the item may never be given or removed. Decide what should happen in that case and write the code for it.

Is this an ESX bug?

Not always. In most reports the error comes from a script that uses the player object after it may have gone. Check the resource that the error names first.

Sources

  • [How to Fix [SOLVED] Attempt to index a nil value (local 'xPlayer'): Server crash (cfx.re forum)](https://forum.cfx.re/t/how-to-fix-solved-attempt-to-index-a-nil-value-local-xplayer-server-crash/100135)
  • xPlayer Functions (ESX Framework docs)

FiveM.tools was founded with sponsorship from Nyru.