table index is nil / index a nil value (field): FiveM Lua fix

Fix 'table index is nil' and 'attempt to index a nil value (field x)' in FiveM Lua: missing config keys, t[nil] assignments, nested tables and safe access patterns.

Two errors that look alike come up all the time in FiveM scripts:

text
SCRIPT ERROR: @my_script/server/main.lua:42: table index is nil
SCRIPT ERROR: @my_script/client/main.lua:19: attempt to index a nil value (field 'shops')

They mean different things. The first is about a nil key, the second about a nil table. Both come down to a value your code expected and did not find. This guide shows how to tell them apart and how to write the access so it cannot crash.

table index is nil

You assigned to a table, and the key was nil:

lua
local cache = {}

local function remember(identifier, data)
    cache[identifier] = data     -- table index is nil when identifier is nil
end

Lua allows any value as a key except nil (and NaN). Reading cache[nil] just gives nil, but writing to it raises the error.

Find which variable between the brackets is nil on that line. The usual sources:

  • A player lookup that returned nothing. GetPlayerIdentifierByType(source, 'license') returns nil when the player has no identifier of that type, for example when the player is not connected through the platform you expect.
  • A wrong source. On the server, check that the event really comes from a player. A call from the console has no player behind it.
  • A field missing in the data: data.id is nil because the sender used data.Id.

Print it to be sure:

lua
print('identifier', identifier, type(identifier))

Then stop the assignment when the key is missing, and decide whether that is an error or a normal case:

lua
local function remember(identifier, data)
    if not identifier then
        return print('remember: no identifier, skipping')
    end
    cache[identifier] = data
end

attempt to index a nil value (field 'x')

text
attempt to index a nil value (field 'shops')

Here the table is missing. The name in brackets is the field that was nil, and the thing you indexed it from is on the line:

lua
local first = Config.shops[1]    -- Config.shops is nil, so [1] fails

Lua names what it knows. A few forms you will see:

Message Meaning
(global 'Config') The global variable Config is nil
(local 'data') A local variable is nil, often a function argument
(field 'shops') Config.shops is nil
(field '?') A table found by a variable key, such as Config.Shops[id], is nil
(upvalue 'cache') A variable from an outer scope is nil

For field '?', Lua cannot name the key because it is computed:

lua
Config.Shops[id].label          -- attempt to index a nil value (field '?')

The fix is the same: find which step of the chain is nil. Print each step:

lua
print(Config)
print(Config.Shops)
print(Config.Shops[id])

Nested config tables

Most of these errors come from config files:

lua
Config = {}
Config.Shops = {
    bar = { label = 'Bar', items = { 'beer', 'water' } },
}

-- somewhere else
local items = Config.Shops[shopName].items

If shopName is 'Bar' instead of 'bar', Config.Shops[shopName] is nil and the next .items crashes. Causes to check:

  1. Different spelling or case. Lua keys are case sensitive.
  2. The config file is not loaded. It must be in shared_scripts (or the right side's list) of fxmanifest.lua, before the file that reads it.
  3. The config is defined on one side only. A file listed under server_scripts is invisible to the client.
  4. A numeric key against a string key. Config.Shops[1] and Config.Shops['1'] are different entries. This often happens after data passes through JSON, see FiveM json.decode errors.
lua
-- fxmanifest.lua
shared_scripts { 'config.lua' }
client_scripts { 'client/*.lua' }
server_scripts { 'server/*.lua' }

Safe access patterns

When a missing value is a normal case, write the access so it handles it.

Check each step with and:

lua
local shop = Config.Shops and Config.Shops[shopName]
if not shop then
    return print(('Unknown shop: %s'):format(tostring(shopName)))
end
print(shop.label)

Use or for a default:

lua
local items = (shop and shop.items) or {}
local radius = Config.Radius or 5.0

Make a small helper when you read deep paths often:

lua
local function get(t, ...)
    for _, key in ipairs({ ... }) do
        if type(t) ~= 'table' then return nil end
        t = t[key]
    end
    return t
end

local label = get(Config, 'Shops', shopName, 'label')

Warning: do not hide every nil with or {}. If a table that must exist is missing, that is a bug in the config or the load order, and a quiet fallback only moves the error somewhere less obvious. Use guards for values that can legitimately be absent.

Dump a table to see what is inside

When you are not sure what a table holds, print it as JSON:

lua
print(json.encode(Config.Shops, { indent = true }))

You will see the real keys and spot a typo, a different case or a number where you expected a string. Do it at the point where the error happens, not at the top of the file, because the data may be filled later.

Checklist

Symptom Fix
table index is nil The key in t[key] = value is nil; print it and return early if it is missing
attempt to index a nil value (field 'shops') The named field is not set in the table before it; check the name and spelling
attempt to index a nil value (field '?') A lookup with a variable key found nothing; check the key and its case
Works on the server, nil on the client The config is not in shared_scripts
Config file ignored List it before the files that use it
Number key vs string key [1] and ['1'] differ; convert with tonumber or tostring

For the other common nil messages, see attempt to compare nil and bad argument errors.

Quick answers

What does 'table index is nil' mean?

The script tried to write to a table using nil as the key, such as t[key] = value when key is nil. Lua does not allow nil as a key, so it stops.

What is the difference between 'table index is nil' and 'attempt to index a nil value'?

In 'table index is nil' the key is nil. In 'attempt to index a nil value' the table itself is nil, so there is nothing to read from or write to.

Why does the error say field '?' instead of a name?

When the key is a variable such as Config.Shops[id], Lua cannot name it. It shows ?. Look at the line and find which part of the chain is nil.

Scripts that skip this problem

Shop CreatorBuild a shop in under a minute β€” owners, employees, vaults and robberies included.View script β†’Quest CreatorA visual editor for quests and NPC dialogues, built node by node in game.View script β†’Item Creator V2Create usable items with animations, props, effects and more β€” without writing code.View script β†’

Keep reading