Error

attempt to index a nil value (global 'ESX')

The most common ESX Legacy error. We explain the cause and the definitive fix so ESX loads before your resource.

The problem

The server console shows "attempt to index a nil value (global 'ESX')" and your resource does nothing or crashes on startup.

The cause

Your script tries to use ESX before the es_extended resource is loaded, or the ensure order in server.cfg is wrong. In modern ESX Legacy the global ESX variable can be empty if you don't retrieve it with getSharedObject.

The solution

Get the ESX shared object safely and wait until it's ready:

lua
-- server.lua / client.lua (modern ESX Legacy)
ESX = exports['es_extended']:getSharedObject()

-- If it still comes back nil on startup, wait for it:
local ESX = nil
CreateThread(function()
    while ESX == nil do
        ESX = exports['es_extended']:getSharedObject()
        Wait(0)
    end
    -- From here on ESX is guaranteed
end)

Step by step

  1. 1.In server.cfg, make sure `ensure es_extended` comes BEFORE `ensure your_resource`.
  2. 2.Use `exports['es_extended']:getSharedObject()` instead of assuming the global ESX.
  3. 3.If your resource depends on ox_lib or another library, declare it earlier in the load order too.
  4. 4.Restart the whole server (not just the resource) to apply the ensure order.

Different case?

Paste your error in the AI tool and get the fix instantly.

Try the tool

Related guides

Last updated: 2026-06-15. Crxative-M is not affiliated with Cfx.re or Rockstar Games.

How to fix "attempt to index a nil value (global 'ESX')" in FiveM