Skip to main content

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:

TypeDescription
nilAbsence of a useful value
booleantrue or false
numberInteger (64-bit) or float (double)
stringImmutable byte sequence, 8-bit clean
functionFirst-class callable (LuaLuaA lightweight, embeddable scripting language used as the primary scripting language in FiveM server development. or C)
userdataOpaque C data allocated by LuaLuaA lightweight, embeddable scripting language used as the primary scripting language in FiveM server development.
threadCoroutine (independent execution thread)
tableAssociative array — the only data structure

[!important] Truthiness Only nil and false are falsy. Everything else — including 0 and "" — 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:

SubtypeDefaultSize
integer64-bit signed-2^63 to 2^63 - 1
floatdouble-precisionIEEE 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 → Tostringnumberboolean
numbertostring(n)always truthy/falsy
stringtonumber(s) if numericalways truthy/falsy
booleanerror (no auto)error (no auto)
nilerror (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" + 1 raises an error. Use tonumber("10") + 1.