How to read a FiveM SCRIPT ERROR: file, line and stack trace
Learn to read a FiveM SCRIPT ERROR: the resource and file path, the line number, the stack trace, F8 versus the server console, and how to add prints to find the cause.
A typical error in the console:
SCRIPT ERROR: @my_script/server/main.lua:42: attempt to index a nil value (local 'xPlayer')
> handler (@my_script/server/main.lua:42)
> ref (citizen:/scripting/lua/scheduler.lua:...)It looks like noise, but every part has a meaning, and it usually tells you exactly where to look. This guide shows how to read it, where to find it, and what to do when it is not enough.
The first line: what, where and why
SCRIPT ERROR: @my_script/server/main.lua:42: attempt to index a nil value (local 'xPlayer')SCRIPT ERROR:marks a Lua error in a script. The script keeps running after it, but the code that failed stopped at that line.@my_script/server/main.luais the resource name (my_script) and the path of the file inside the resource folder.:42is the line number in that file.- The rest is the message:
attempt to index a nil value (local 'xPlayer'). Lua reads it like this: on line 42 you tried to use something insidexPlayer, butxPlayerisnil.
Open the file, go to that line, and look at the variables that line uses. In most cases the answer is already there. Common messages have their own guides: attempt to call a nil value and attempt to compare nil.
The stack trace: how the code got there
The lines starting with > are the stack trace. It lists the functions that were running, from the failing one down to the one that started it:
> handler (@my_script/server/main.lua:42)
> ref (citizen:/scripting/lua/scheduler.lua:...)The first line after the error is the same place as the error. Lines below it show who called that function. When the error is inside a library or another resource, a line from your own script further down the list is often the real cause, because your script passed a wrong value into it.
Lines that begin with citizen:/scripting/lua/... (for example citizen:/scripting/lua/scheduler.lua) belong to FiveM itself. They are part of the machinery that runs your events and threads. You can ignore them: the error is never fixed there, and they only show that a thread or an event was running.
Client or server: where to look
A script error appears on the side where the code runs:
- Client scripts (
client_scripts, files in aclientfolder): press F8 in game to open the client console. If you do not see anything, scroll up, as errors can scroll away fast. - Server scripts (
server_scripts, files in aserverfolder): read the server console. In txAdmin, that is the Live Console page. On a server started from a terminal, it is that terminal window. - Shared scripts run on both sides, so the error can show up in either place.
Tip: if a menu or an action does nothing and F8 is empty, check the server console. The failing code may be on the server side.
Which line to look at
- Read the message first: it says what kind of problem it is (
nil, a wrong type, a missing export). - Open the file and line from the first line. Check every variable on it.
- If the file belongs to a library (ox_lib, the framework), go down the stack trace until you reach a line of the script you are fixing.
- Check if the error appears once or constantly. A constant error in a loop floods the console. Fix that one first, as it hides other messages.
Errors with the text No such export or could not find dependency are different. They are about resources, not lines of code. See could not find dependency.
When the line is not enough: add prints
If the line uses several variables and you do not know which one is nil, print them just before it:
print('xPlayer:', xPlayer)
print('source:', source, type(source))
print('data:', json.encode(data))Run the code again and read the console. Some tips for useful prints:
- Print the type as well as the value:
type(value)showsnil,number,string,tableorfunction. - Use
json.encode(table)to see the inside of a table. A plainprint(table)only shows a memory address. - Put a short label in front, so you can find the line in a busy console.
- Print at the start of a function, to confirm it runs at all.
Remove the prints once the problem is fixed, as they cost performance in busy loops.
Where an error does not appear
- Escrowed scripts hide their code. The error gives you a file name, but the file is encrypted and you cannot read the line. Contact the author with the full error text.
- NUI errors (the menu itself, written in HTML and JavaScript) do not show in F8 as
SCRIPT ERROR. Open the NUI developer tools to read them: thenui_devtoolscommand in the F8 console opens them in a browser tab. - Errors in the first seconds: when you start the server, errors from resources that fail to load can pass quickly. Search the console or scroll back up, or restart only the one resource and watch.
What to send when you ask for help
Copy the complete error, the stack trace included, as text, and say whether it comes from F8 or from the server console. Add the line of code it points to and the framework you use. A screenshot of one cropped line is not enough for anyone to help you.
Checklist
| Symptom | Fix |
|---|---|
@resource/path.lua:42 |
Open that file in that resource and go to line 42 |
| Error in a library file | Follow the stack trace to the first line of your own script |
citizen:/scripting/lua/... lines |
FiveM internals: ignore them |
| Nothing in F8 but the feature fails | Read the server console |
| Several variables on the failing line | Print each one with type() and json.encode() |
| Error file is unreadable | Escrowed script: send the full error text to the author |
| NUI menu fails | Open nui_devtools and read the browser console |
Quick answers
Where do I see a client script error?
Press F8 in game to open the client console. Errors from client scripts show there in red, with the resource name and the line.
Where do I see a server script error?
In the server console, which is the Live Console in txAdmin or the terminal window where the server runs. Server errors do not show in F8.
The error points to a file in another resource. Is that script broken?
Not always. Look further down the stack trace: the line that calls into that resource, with wrong or missing arguments, is often the real cause.
Scripts that skip this problem
Mic PhoneA foldable phone that unfolds into a tablet and carries onto a player's real phone.View script →
CCTV Security CamerasPlaceable cameras, a live multi-view tablet and printed evidence photos.View script →
Advanced BoostingTablet-driven vehicle boosting: contracts from class D to S+, crews and a live queue.View script →