Lua for FiveM: the basics, common mistakes and the AI skill
Updated
Don’t want to manage skills yourself? Get the full app.
FiveM scripts are written in Lua, so functions, tables, variables, conditionals and error handling decide whether a resource works. This guide covers the Lua mistakes that break resources and the AI skill that writes idiomatic Lua.
Lua for FiveM is a small language with a few sharp edges, and most broken resources trip on those edges rather than on the FiveM API. Arrays start at 1, nil and false are different, there is no continue and no ternary. If you write or review Lua resources, or you let an AI assistant write them for you, you need these rules in front of you. The Lua basics skill packs them into rule files the assistant reads before writing code.
What the Lua basics skill gives you
The SKILL.md is an index. Each topic has its own rule file with explanations and examples.
rules/functions.md. Function size, naming, parameters, exports and guard clauses. Short functions that return early instead of nesting.rules/tables.md. Array indices start at 1, dereferencing, avoidingtable.insertin hot paths, iteration withipairsandpairs, and array size with#.rules/variables.md. Naming for constants, locals over globals, and when an enum reads better than a boolean.rules/conditionals.md. Default values withor, boolean expressions and readability.rules/errors.md. Assertions, pre-conditions, errors as values and failing loudly instead of swallowing problems.rules/reference-links.md. Official Lua and FiveM documentation, including docs.fivem.net.
Common mistakes with Lua for FiveM
- Accidental globals. A missing
localturns a variable into a global shared by every file in the resource. Symptom: values leak between scripts, a counter in one file changes in another, and global lookups are slower than locals. Fix: declare everything withlocaland treat a bare assignment as a bug. - Indexing from 0.
players[0]isniland afor i = 0, #playersloop starts on an empty slot. Symptom: the first entry is skipped, or the code errors on iteration zero. Fix: start at 1 and iterate withipairs. - Using
#on the wrong table.#tis only defined for a sequence without holes. It ignores string keys, and with holes the result is unpredictable. Fix: keep a separate counter or iterate withpairswhen the keys are not 1..n. - Confusing
falsewithnil.if x thenfails for bothnilandfalse. Iffalseis a valid value, a default likex = x or truesilently overrides it. Fix: writeif x ~= nil thenwhenfalsematters, and be careful witha and b or cwhenbcan befalse. - Treating Lua like JavaScript. There is no
!=(use~=), nocontinue(usegoto continueor restructure), no ternary. Building big strings with..inside loops creates a new string on every step. Fix: usetable.concatfor large output and write conditionals the Lua way.
Why AI assistants get Lua for FiveM wrong
A generic assistant writes Lua with the muscle memory of JavaScript and Python. It drops local, starts loops at 0, uses !=, invents a continue, and reaches for a and b or c as a ternary without noticing the false case. It also calls table.insert inside hot server loops because that is what every tutorial does. The code often runs and fails later, quietly, under load.
The Lua basics skill gives the assistant the five rule files and tells it to read the one that matches the task. Functions get guard clauses and small bodies, tables get 1-based loops with ipairs, variables are local by default, errors surface through assertions and pre-conditions instead of being ignored. The result reads like Lua written by someone who works on FiveM every day, which makes it far easier for you to review.
Install the Lua basics skill in 30 seconds
- Click "Download SKILL.md" at the top of this page.
- Unzip it into the skills or rules folder of your AI tool. The install section below shows the exact path for Claude Code, Cursor and VS Code.
- Ask the assistant for something concrete, like "write a function that returns the players within 10 meters of a coordinate", and check that every variable is
local, the loop usesipairsand the function returns early on bad input.
How to install this skill
Download the zip, unzip it, and place the folder where your tool loads skills or rules. The exact path depends on the tool:
Claude Code
Unzip into .claude/skills/lua-basics/ inside the project (the folder must contain SKILL.md). For every project, use ~/.claude/skills/lua-basics/ instead.
Cursor
Cursor loads project rules from .cursor/rules/. Add a rule file there that points to the unzipped skill, or paste the contents of SKILL.md and the rule files into a rule. The format is .mdc with frontmatter and changes between versions, so check the docs for your version.
VS Code / Copilot
Copilot reads instruction files from .github/instructions/*.instructions.md. Copy SKILL.md there with an applyTo glob for your Lua files, and keep the rule files next to it.
The SKILL.md file
This is the file your AI assistant reads. It links to the rule files bundled in the download.
Lua Basics
Best practices for writing effective Lua code in FiveM. This skill covers fundamental Lua patterns, performance optimizations, and code quality guidelines specifically for FiveM development.
When to use
- Writing or reviewing Lua code for FiveM resources.
- Questions about Lua best practices, naming conventions, or code structure.
- Optimizing Lua code for performance.
- Need guidance on functions, tables, variables, conditionals, or error handling in Lua.
How to use
Read individual rule files for detailed explanations and examples:
- rules/functions.md — Function best practices: size, naming, parameters, exports, guard clauses.
- rules/tables.md — Table operations: array indices, dereferencing, avoiding table.insert, iterations, array size.
- rules/variables.md — Variable naming conventions: constants, locals, globals, enums vs booleans.
- rules/conditionals.md — Conditional patterns: default values, boolean expressions, readability.
- rules/errors.md — Error handling: assertions, pre-conditions, errors as values, fail loudly.
- rules/reference-links.md — Official Lua and FiveM documentation resources.
Frequently asked questions
- Current FXServer builds run Lua 5.4 with FiveM-specific additions. Older resources written for 5.3 mostly still work, but check integer division and other 5.4 changes before copying old code.
- You should read it well enough to review what the assistant writes. AI can produce a working resource, but it also produces accidental globals and off-by-one loops. If you cannot spot those, you cannot trust the output.
- Yes, with a manual step. The SKILL.md and its rules folder are plain markdown, so you can paste them into the conversation as context. Native skill support is for Claude Code, Cursor and VS Code.
Which Lua version does FiveM use?
Do I need to know Lua to use AI for FiveM?
Can I use this skill with ChatGPT?
Related guides
Longer reads from the FiveAI blog on the same topic.