attempt to index a nil value (global 'QBCore'): the GetCoreObject fix

QBCore is nil in your script? The old QBCore:GetObject event is gone. How to get the core object with GetCoreObject, on QBCore and on QBox, and why it can still be nil.

Somewhere in your console:

text
SCRIPT ERROR: @my_script/client/main.lua:8: attempt to index a nil value (global 'QBCore')

QBCore is running, the script looks fine, and QBCore is still nil. Here is why, and how to fix it on QBCore and on QBox.

Why QBCore is nil

Older QBCore scripts got the core object with an event:

lua
QBCore = nil
TriggerEvent('QBCore:GetObject', function(obj) QBCore = obj end)

That event was removed from qb-core. Nobody answers it any more, so QBCore stays nil and the first QBCore.Functions… line crashes.

The fix: GetCoreObject

Ask qb-core for its object with the export, at the top of the file:

lua
local QBCore = exports['qb-core']:GetCoreObject()

It is the same line on the client and on the server. Put it in every file that uses QBCore, or once in a shared file that the manifest loads first:

lua
-- fxmanifest.lua
shared_script 'shared/core.lua'
client_scripts { 'client/*.lua' }
server_scripts { 'server/*.lua' }
lua
-- shared/core.lua
QBCore = exports['qb-core']:GetCoreObject()

Tip: if QBCore is local in one file, the other files cannot see it. A nil error in only one file of the script usually means exactly that.

Still nil? qb-core is not running yet

The export only answers if qb-core started first. In server.cfg:

cfg
ensure oxmysql
ensure ox_lib
ensure qb-core
ensure [qb]

ensure my_script

Then:

  • Check qb-core's own start at the top of the server console. A database error there means its exports never exist, and you get No such export GetCoreObject in resource qb-core.
  • Add the dependency to the script's fxmanifest.lua, so FiveM starts qb-core first or refuses to start the script without it:
lua
dependency 'qb-core'

On QBox

QBox's core is qbx_core, and it keeps a compatibility layer for qb-core scripts: exports['qb-core']:GetCoreObject() still returns an object, so most QBCore scripts run as they are.

What changes is the order and the name in server.cfg:

cfg
ensure oxmysql
ensure ox_lib
ensure qbx_core

There is no separate qb-core folder on QBox. If you still have one, remove it, or the two cores fight.

For new code, QBox prefers its own exports over the big QBCore table, for example on the server:

lua
local player = exports.qbx_core:GetPlayer(source)
if player then
    print(player.PlayerData.citizenid)
end

Player data on the client

Reading the player too early is the other classic nil:

lua
-- this runs before the player is loaded: job is nil
local job = QBCore.Functions.GetPlayerData().job

Wait for the player instead:

lua
local QBCore = exports['qb-core']:GetCoreObject()
local PlayerData = {}

RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
    PlayerData = QBCore.Functions.GetPlayerData()
end)

RegisterNetEvent('QBCore:Client:OnJobUpdate', function(job)
    PlayerData.job = job
end)

If the script can also be restarted while you are in game, read the data once when it starts as well, since OnPlayerLoaded has already fired by then.

Checklist

Symptom Fix
attempt to index a nil value (global 'QBCore') local QBCore = exports['qb-core']:GetCoreObject() at the top of the file
Nil in one file only QBCore is local to another file; define it there too, or in a shared file
No such export GetCoreObject qb-core (or qbx_core) not started, or failed to start
QBox server Keep the export, start qbx_core, remove any old qb-core folder
job is nil at startup Wait for QBCore:Client:OnPlayerLoaded

Running ESX instead? The same problem there is ESX is nil: fixing esx:getSharedObject.

Quick answers

Why is QBCore nil in my script?

The script never got the core object: it uses the removed QBCore:GetObject event, it defines QBCore in another file, or it starts before qb-core. Use local QBCore = exports['qb-core']:GetCoreObject() at the top of each file.

Does GetCoreObject work on QBox?

Yes. qbx_core keeps a compatibility layer for qb-core, so exports['qb-core']:GetCoreObject() still answers. For new code, QBox prefers its own exports, such as exports.qbx_core:GetPlayer(source).

Do I have to call GetCoreObject in every file?

Each file that uses QBCore needs it, unless the script defines it once in a shared file that is loaded before the others.

Scripts that skip this problem

Advanced BoostingTablet-driven vehicle boosting: contracts from class D to S+, crews and a live queue.View script →Clothing DesignerDesign clothing inside FiveM — brush, layers, image import and AI — then wear it.View script →Quest CreatorA visual editor for quests and NPC dialogues, built node by node in game.View script →

Keep reading