ScriptsAboutBlogToolsKnowledge BaseReviewsFAQBasketDocsSupport
Luacrash

attempt to index a nil value

Also seen as: attempt to index a nil value (global 'ESX') attempt to index a nil value (global 'QBCore') attempt to index a nil value (local 'xPlayer') attempt to index a nil value (field 'Functions')

What causes it

Your code tried to read a field on something that is nil — a variable expected to hold a table or object doesn't exist yet. The message names the variable in parentheses, which is your fastest clue. The usual culprits are: a framework object (ESX/QBCore) that hasn't initialized before your script runs, a missing Config value, a load-order problem inside your own resource, a typo, or a player who disconnected mid-script.

How to fix it

Read the variable name in parentheses — that's what is nil. Then trace why it's nil at that point.

  1. Look at the file and line in the error (e.g. @my_resource/server/main.lua:42) and read the code around it.
  2. If the nil is ESX or QBCore: your framework object isn't loaded yet. On ESX 1.9+, use ESX = exports['es_extended']:getSharedObject() instead of the old TriggerEvent('esx:getSharedObject') pattern.
  3. If the nil is Config or a key inside it: in fxmanifest.lua, make sure config.lua is listed in shared_scripts BEFORE any file that reads it.
  4. If the nil is a player object (xPlayer, Player): the player may not exist yet or disconnected. Guard it: if not xPlayer then return end.
  5. If it's a dependency's export: make sure that resource is started before yours in server.cfg.

Related errors

Related guides

Still stuck? We can help.

Optimized, dual-framework scripts for QBCore and ESX from Viper Development — escrow protected, delivered instantly to your Cfx portal.