Types & Values
LuaLuaA lightweight, embeddable scripting language used as the primary scripting language in FiveM server development. is dynamically typed. Every value carries its own type. There are eight basic types:
| Type | Description |
|---|---|
nil | Absence of a useful value |
boolean | true or false |
number | Integer (64-bit) or float (double) |
string | Immutable byte sequence, 8-bit clean |
function | First-class callable (LuaLuaA lightweight, embeddable scripting language used as the primary scripting language in FiveM server development. or C) |
userdata | Opaque C data allocated by LuaLuaA lightweight, embeddable scripting language used as the primary scripting language in FiveM server development. |
thread | Coroutine (independent execution thread) |
table | Associative array — the only data structure |
[!important] Truthiness Only
nilandfalseare falsy. Everything else — including0and""— is truthy.
nil
Represents "no value". Unset table fields return nil. Global variables default to nil.
print(x) -- nil (x not defined)
local t = {}
print(t[1]) -- nil (key not present)
boolean
Two literal values: true and false.
if not false then print("ok") end -- ok
number
Two subtypes, automatically coerced:
| Subtype | Default | Size |
|---|---|---|
| integer | 64-bit signed | -2^63 to 2^63 - 1 |
| float | double-precision | IEEE 754 |
Integer literals
3 345 0xff 0xBEBADA
0xAA -127 0x10 0xdeadBEEF
Float literals
3.0 3.1416 314.16e-2 0x1.fp10
0.3 .3 1.2e-20 0x0.1p-1
Coercion rules
- Arithmetic on two integers → integer
- Arithmetic with at least one float → float
math.type(x)returns"integer"or"float"
print(1 + 2) -- 3 (integer)
print(1 + 2.0) -- 3.0 (float)
print(math.type(3)) -- integer
print(math.type(3.0)) -- float
[!warning] Integer overflow Integer arithmetic wraps around on overflow (two's complement).
string
Immutable sequences of bytes. Encoding-agnostic (works with UTF-8 but doesn't enforce it).
Literal forms
-- Single or double quotes
'hello'
"hello"
-- Escape sequences
'alo\n123"' -- newline, escaped quote
'\97lo\10\04923"' -- decimal byte codes
-- Long strings (brackets, ignore leading newline)
[[multi
line
string]]
-- Nested brackets (any level of = signs)
[==[can contain ]] inside]==]
String operations
#s -- length (bytes)
s1 .. s2 -- concatenation (creates new string)
string.byte(s) -- byte values
string.char(97) -- "a"
tostring(x) -- convert to string
tonumber("42") -- convert to number
[!tip] Strings are interned Equal strings share the same memory. Comparing by content is O(1).
function
First-class values. Can be stored in variables, passed as arguments, returned from functions.
local f = function(x) return x * 2 end
print(f(21)) -- 42
See Functions-Closures for full details.
userdata
Opaque C data managed by LuaLuaA lightweight, embeddable scripting language used as the primary scripting language in FiveM server development.. Two kinds:
- Full userdata: garbage-collected block allocated by LuaLuaA lightweight, embeddable scripting language used as the primary scripting language in FiveM server development.
- Light userdata: plain C pointer (not managed)
Userdata can have metatables to define behavior.
thread
Represents a coroutine. Not an OS thread — cooperative multitasking only.
See Coroutines for full details.
table
The sole data-structuring mechanism. Implements associative arrays.
local t = {}
t["key"] = "value"
t[1] = "first"
t.name = "Lua" -- sugar for t["name"]
See Tables for full details.
Type coercion summary
| From → To | string | number | boolean |
|---|---|---|---|
| number | tostring(n) | — | always truthy/falsy |
| string | — | tonumber(s) if numeric | always truthy/falsy |
| boolean | error (no auto) | error (no auto) | — |
| nil | error (no auto) | error (no auto) | falsy |
[!warning] No implicit string↔number Lua 5.4LuaA lightweight, embeddable scripting language used as the primary scripting language in FiveM server development. removed implicit coercion between strings and numbers.
"10" + 1raises an error. Usetonumber("10") + 1.