Fixing "attempt to call a nil value" in FiveM Lua
"attempt to call a nil value" and its cousin "attempt to perform arithmetic on a nil value" are two of the most frequent Lua errors in FiveM script development. They are close relatives of the index-nil error, but the trigger is different: instead of reading a field, you tried to call something that is not a function, or do math on something that is not a number. Both messages name the culprit in parentheses, which is your fastest path to the fix.
This guide explains what each error really means, the common real causes (a misremembered native, a missing export, a wrong load order, a missing config value), and a reliable print-trace-fix routine that resolves both.
"Call a nil value" means you called a non-function
This error fires when you put parentheses after something that is nil: myFunc(), obj:method(), or exports['x']:doThing(). Lua tried to call it, found nothing there, and threw. The parentheses in the message tell you exactly what was missing: (global 'X') means a global function or resource was not defined, (field 'Y') means an object has no method named Y, (method 'Z') means the colon-call target is missing that method.
The most common version with FiveM is calling a native or framework function that is not available in that context. A server-only function called on the client (or vice versa) shows up as a nil call, because that native simply does not exist on that side. Check that the function belongs to the runtime you are in.
- global 'X' -> function X was never defined, or the resource providing it is not running
- field 'Y' -> the object has no member Y (often a typo or a wrong API name)
- method 'Z' -> you used obj:Z() but Z is not a method on obj
- Calling a server native on the client (or vice versa) is a frequent cause
Common real causes and their fixes
A misremembered function name is the simplest case. Calling Vdist() when the function does not exist throws a nil-call. In FiveM, vector math replaced many old helpers: distance is now local d = #(vec1 - vec2). When a native looks wrong, check the current native reference rather than copying an old snippet.
A missing export is the framework version: exports['some_resource']:Foo() throws "call a nil value" if some_resource is not running, is named differently, or does not actually export Foo. Confirm the resource is started, the folder name matches, and the function name is spelled exactly as the resource defines it.
Order matters too. If your code calls a function that another file defines, but that file loads later (or on the other side of client/server), the function is nil at call time. Fix the load order in fxmanifest.lua, or make sure the function is defined before it is used.
"Arithmetic on a nil value" means a number is missing
This sibling error appears when you do +, -, *, or / with a value that is nil. Typically a config value, a database result, or a function return that you assumed was a number turned out to be nil. price * amount throws if either price or amount was never set.
Trace it to the source. Print the operands right before the math: print('price', price, 'amount', amount). Whichever prints nil is the one to fix, and the fix is usually upstream, a missing Config key, a query that returned no row, or a function that returned nothing on some code path.
Guarding is cheap insurance. Default a possibly-missing number: local amount = data.amount or 0. That keeps the math valid and prevents the crash, though you should still ask why the value was nil in the first place, because a silent default can hide a real bug.
A reliable debugging routine
For both errors, the routine is the same. Open the file:line from the message. Identify the named value in parentheses. Decide whether it is a function that should exist (call-nil) or a number that should exist (arithmetic-nil). Then trace that value backward to where it was supposed to be defined or assigned, and fix it there, not at the crash site.
Add temporary print() or Citizen.Trace() calls to see the values flowing into the failing line. Remove them once fixed. This print-trace-fix loop solves the vast majority of nil errors without needing a forum thread.
PlayDeck teaches exactly this loop with an AI build workflow: you paste the error and the surrounding code, the AI explains which value is nil and why, and proposes a fix that you review and apply. You learn the pattern, not just the patch. Join the waitlist to build with it.
Frequently asked questions
What's the difference between "call a nil value" and "index a nil value"?
Index-nil happens when you read a field with a dot (obj.field) on nil. Call-nil happens when you call with parentheses (func()) on nil. One is reaching into nothing; the other is trying to run nothing as a function.
Why does my FiveM native throw "call a nil value"?
Usually because you called it in the wrong context, a server-only native on the client or vice versa, or you used an outdated or misspelled name. Check the current native reference and confirm which side it belongs on.
How do I fix "arithmetic on a nil value"?
One of the numbers in the math is nil. Print the operands just before the line to find which one, then fix it upstream (a missing config key, an empty query result). A default like (value or 0) stops the crash but verify why it was nil.
My export call returns nil. What should I check?
Confirm the resource providing the export is running and started before your script, that its folder name matches what you typed in exports['name'], and that the function name is spelled exactly as the resource defines it.