Fix “attempt to index a nil value” in FiveM scripts (ESX, QBCore, xPlayer)
This Lua error means a script used something that does not exist as if it were a table. The part in brackets, such as global 'ESX' or local 'xPlayer', tells you which thing is missing, and each one has a different fix.
Read the error first
The server console and the F8 console show the error like this:
SCRIPT ERROR: @my_script/server/main.lua:12: attempt to index a nil value (global 'ESX')
The resource name, the file and the line number come first, so you know exactly where to look. The words in brackets say what is nil:
| In the brackets | What it usually means |
|---|---|
| global 'ESX' | The script never got the ESX object, or ESX was not running when the script started. |
| global 'QBCore' | The same for QBCore. |
| local 'xPlayer' or 'Player' | The framework has no player object for that player. |
| field 'Jobs' or another name | The table exists but the key is missing, because data or a config file was not loaded. |
Fix global 'ESX'
- Start ESX first. In server.cfg, put
ensure es_extendedabove every script that uses it, and check the console for errors from es_extended itself, which needs its database connection to start. - Import ESX in the script. Add this line to the script's fxmanifest.lua:
shared_script '@es_extended/imports.lua'
Or set the object in Lua, near the top of the file:
ESX = exports["es_extended"]:getSharedObject()
Old scripts use TriggerEvent('esx:getSharedObject', ...). That event no longer exists in current ESX, so replace it with the export above.
Fix global 'QBCore'
Define the object at the top of every Lua file that uses it:
local QBCore = exports['qb-core']:GetCoreObject()
A local in one file is not visible in another, so a script that works in client/main.lua can still fail in client/events.lua. Start qb-core before the script in server.cfg. Qbox includes a compatibility layer for most QBCore scripts, but check that the script supports it.
Fix local 'xPlayer'
ESX.GetPlayerFromId(source) and QBCore.Functions.GetPlayer(source) return nil when the player is not loaded yet, has left, or the ID is wrong. Always check the result before you use it:
local xPlayer = ESX.GetPlayerFromId(source) if not xPlayer then return end
Common reasons the player is missing:
sourceis 0 when a command runs from the server console, so there is no player.sourcecan change after aWait. Copy it first withlocal src = sourceand usesrcafterwards.- The event ran while the player was still joining. Wait for the framework's player-loaded event,
esx:playerLoadedin ESX andQBCore:Server:PlayerLoadedin QBCore. - The player is offline. Look them up by identifier in the database instead.
Fix a missing field
If a table exists but a key is nil, the data behind it was not loaded. Check that the config file is listed in fxmanifest.lua before the files that use it, that the name is spelled the same everywhere, and that the script version matches your framework version. Frameworks rename functions between versions, and scripts written for an older version break.
Check the basics
- Read the first error in the console. Later errors are often a result of the first.
- Check the script's own framework setting, if it has one, in its config file.
- After editing a script, run
restart resource_namein the console and watch for new errors.
Still stuck?
Ask the script's author with the full error text, the file and line, your framework and its version, and the start order from server.cfg.
Host your FiveM server in Singapore
Low ping for players in India, Malaysia and Indonesia, on AMD EPYC with NVMe and Gcore DDoS protection on every IP.
