FiveM "attempt to index a nil value": QBCore, ESX & ox_lib Fix
“attempt to index a nil value” means your Lua code tried to read a field (like Player.PlayerData) from a variable that is nil. The error tells you the file, the line and usually the variable name. In QBCore and ESX the usual culprits are a player object fetched before the player has loaded, a core object that was never set, a wrong export name, or a resource starting before its dependency. Find the nil variable, work out why it is nil, then fix the cause or add a guard.
Step 1: Read the error properly
A typical server console line looks like this:
SCRIPT ERROR: @my-job/server/main.lua:42: attempt to index a nil value (local 'Player')my-jobis the resource.server/main.lua:42is the file and line.(local 'Player')names the variable that was nil. It might also sayfield,globalorupvalue, which tells you where the name came from.
Client errors show in the F8 console; server errors in the server console or txAdmin. See FiveM server console errors.
Step 2 (QBCore): the Player object is nil
QBCore.Functions.GetPlayer(source) returns nil when there is no loaded player for that source: the event fired before character selection, the player dropped, or the wrong ID was passed.
local QBCore = exports['qb-core']:GetCoreObject()
RegisterNetEvent('my-job:server:pay', function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end -- guard: no loaded player
Player.Functions.AddMoney('cash', 100, 'my-job-pay')
end)Two related slips: using source after a Wait() (copy it to a local first, as above), and trusting a client-supplied ID instead of source, which is also a security risk. On the client, wait for QBCore:Client:OnPlayerLoaded before reading PlayerData.
Step 3: the core object itself is nil
If the error names QBCore or ESX as a global, the core object was never set. Check the line that fetches it runs before use and that the resource name in the export matches your server exactly:
-- QBCore
local QBCore = exports['qb-core']:GetCoreObject()
-- ESX (Legacy)
local ESX = exports['es_extended']:getSharedObject()Old scripts using TriggerEvent('esx:getSharedObject', ...) may not work on current ESX Legacy; check the ESX documentation. Make sure the core resource starts before yours in server.cfg and list it under dependencies in fxmanifest.lua. See QBCore export not found.
Step 4 (ESX): xPlayer is nil
RegisterNetEvent('my-job:server:pay', function()
local xPlayer = ESX.GetPlayerFromId(source)
if not xPlayer then return end
xPlayer.addMoney(100)
end)Same cause as QBCore: the player isn’t loaded yet or the ID is wrong.
Step 5 (ox_lib): lib is nil
If the variable is lib, ox_lib isn’t loaded into your resource. As the ox_lib documentation describes, add it to fxmanifest.lua and make sure ox_lib starts first:
fx_version 'cerulean'
game 'gta5'
lua54 'yes'
shared_script '@ox_lib/init.lua'
dependencies { 'ox_lib' }More in installing ox_lib with QBCore.
Step 6: a field in your own data is nil
If the error says (field 'something'), a table lookup returned nil: a missing config entry, an item not in your items list, or a database row that doesn’t exist. Print the table before the failing line:
print(json.encode(Config.Jobs, { indent = true }))
local job = Config.Jobs[jobName]
if not job then
print(('[my-job] unknown job: %s'):format(tostring(jobName)))
return
end
Quick checklist
- Read the file, line and variable name from the error.
- Player/xPlayer nil? Guard it and check the player has loaded.
- QBCore/ESX nil? Fix the export name and start order.
- lib nil? Add
@ox_lib/init.luato the manifest. - Field nil? Print the table and check config, items and database.
FAQ
What does “attempt to index a nil value” mean in FiveM?
Your Lua code tried to read a field from a variable that is nil. The error gives the file, line and usually the variable name.
Why is QBCore.Functions.GetPlayer returning nil?
There is no loaded player for that source: the event ran before the character loaded, the player left, or the wrong ID was passed.
Why is my QBCore or ESX object nil?
The line that fetches the core object didn't run, the export or resource name is wrong, or the core resource starts after yours.
Why does ox_lib say lib is nil?
Add shared_script '@ox_lib/init.lua' to fxmanifest.lua and make sure ox_lib starts before your resource.
Should I add nil checks everywhere?
Guard where nil is a normal case, such as a player who has left, but also fix the real cause or the feature will silently do nothing.
Can AI fix this error?
Stellar AI can explain the error and suggest corrected Lua if you paste the full error, the script and your framework. Start free with up to 15 Stellar Fast replies a day and test the fix on a dev server first.
Related guides
- QBCore Nil Value Error: Index Nil Fixes
- QBCore Inventory Item Not Showing? Fixes
- QBCore Money Not Saving? Cash & Bank Fixes
- QBCore ox_target Not Working — FiveM Fixes
Want it fixed for you? The £99 Priority Script Fix is a one-off fix for a FiveM or Roblox script. Memberships start at £8 a month on the plans page.
Related tools: FiveM script generator · Fix a FiveM Lua error · all script generators