attempt to call a nil value (field, global, method): how to fix it

Getting 'attempt to call a nil value' in FiveM? What the field, global and method variants mean, and how to fix a typo, a missing export or a colon-versus-dot mistake.

In the console:

text
SCRIPT ERROR: @my_script/server/main.lua:42: attempt to call a nil value (field 'GetPlayerFromId')
SCRIPT ERROR: @my_script/client/main.lua:17: attempt to call a nil value (global 'DrawText3D')
SCRIPT ERROR: @my_script/client/main.lua:63: attempt to call a nil value (method 'Notify')

Lua is telling you that a line tried to run a function that does not exist. This article explains the three variants, the usual causes, and how to fix each one.

What the error means

In Lua a missing name is not an error by itself: reading it gives nil. The error only appears when you put brackets after it, because you cannot run nil. The text in brackets says how the script reached the missing function:

  • attempt to call a nil value (global 'X'): a bare call like X(). The function is not defined anywhere the script can see.
  • attempt to call a nil value (field 'X'): a call through a table like ESX.X() or QBCore.Functions.X(). The table exists, but it has no entry named X.
  • attempt to call a nil value (method 'X'): a call with a colon like obj:X(). The object exists, but has no method X.

The file and line in front of the message (@my_script/server/main.lua:42) is the line that makes the call. If you are not sure how to read the rest of the output, see how to read a FiveM script error.

1. A typo in the name

Lua is case sensitive. GetPlayerFromId and getPlayerFromId are two different names, and so are TriggerClientEvent and TriggerclientEvent. Compare the name in the error with the framework documentation, letter by letter.

2. The function does not exist in your version

Frameworks rename and remove functions. This is the most common cause on scripts that were written for another version of your framework.

lua
-- an old call that may be gone on your version
local xPlayer = ESX.GetPlayerFromId(source)
xPlayer.getInventoryItem('bread')

Check what your framework really offers. Print the table to see its entries:

lua
for name, value in pairs(ESX) do
    print(name, type(value))
end

If the function you need is not in the list, the script and the framework do not match. Update the framework, update the script, or replace the call with the one your version has.

3. The export is missing

Exports are functions that another resource provides. When the resource is stopped, failed to start, or does not export that name, the call fails.

lua
local core = exports['qb-core']:GetCoreObject()
core.Functions.GetPlayer(source)

If the resource is not running, FiveM prints No such export instead. If it runs but the export name is wrong, you can end up with a nil field. Start the resource first in server.cfg, and check the exact export names in its documentation or fxmanifest.lua. Two related fixes: ESX is nil and No such export getSharedObject.

4. Colon versus dot

A colon passes the object as a hidden first argument. A dot does not. Mixing them gives either this error or wrong arguments.

lua
-- calls the method with the object as first argument
player:Notify('hello')

-- calls a plain function stored in a table
Utils.Notify('hello')

If the error says (method 'Notify'), the object has no Notify entry: use the dot form if the function lives on the library table, or check the object really is the thing you think it is. With ox_lib, for example, lib.notify({...}) is a dot call on the lib table.

5. The function is defined later or in another file

A local function must be defined before the line that calls it, in the same file. A function defined in another file is only visible if it is global, and only if that file is loaded by the same side (client or server) of the same resource.

lua
-- fails: DoThing is defined below the call
DoThing()

local function DoThing()
    print('hi')
end

Move the definition above the call, or declare it first with local DoThing and assign it later. Check fxmanifest.lua too: a file that is not listed in client_scripts, server_scripts or shared_scripts never loads, so its functions never exist.

lua
client_scripts { 'client/*.lua' }
server_scripts { 'server/*.lua' }
shared_scripts { '@ox_lib/init.lua', 'config.lua' }

6. A client function used on the server, or the other way round

Many natives and framework functions only exist on one side. PlayerPedId is client only, and GetPlayerIdentifiers is server only. Framework tables differ too: ESX.GetPlayerFromId is a server function, while the client uses ESX.GetPlayerData. A script that runs in shared_scripts runs on both sides, so a call that works on one side crashes on the other.

Tip: the quickest test is a print right before the failing line: print(type(ESX.GetPlayerFromId)). If it prints nil, the function is missing. If it prints function, the real problem is somewhere else.

Guard it while you debug

Check before you call, so you get a clear message instead of a crash:

lua
local fn = ESX.GetPlayerFromId
if type(fn) ~= 'function' then
    print('GetPlayerFromId is missing: check the ESX version')
    return
end

Do not leave guards like this in place as the fix. They hide the real problem, which is a script and a framework that do not match.

Checklist

Symptom Fix
(global 'X') The function is not defined in the file or never loaded: check the manifest and the definition order
(field 'X') The table has no such entry: check the spelling and the framework version
(method 'X') Wrong object or colon used instead of a dot: check what the variable holds
Works on one side only The function exists only on client or only on server
Error after a framework update Replace the removed function with the new one, or update the script
Export call returns nil Start the providing resource first and check the export name

Quick answers

What does 'attempt to call a nil value' mean?

The script tried to run something as a function, but the name holds nil, so nothing is there to run. The name inside the brackets tells you which function is missing.

What is the difference between field, global and method in this error?

A global is a bare name like Foo(), a field is a table entry like Config.Foo() or ESX.Foo(), and a method is a call with a colon like obj:Foo(). It only tells you how the missing function was reached.

Can a wrong framework version cause it?

Yes. A script written for an older or newer ESX, QBCore or QBox may call a function that your version renamed or removed, which leaves nil behind.

Scripts that skip this problem

Advanced BoostingTablet-driven vehicle boosting: contracts from class D to S+, crews and a live queue.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