Standard Library
LuaLuaA lightweight, embeddable scripting language used as the primary scripting language in FiveM server development.'s standard library is organized into modules. Load them with require.
Basic functions (global)
| Function | Description |
|---|---|
assert(v [, msg]) | Raise error if v is nil/false |
collectgarbage(opt [, arg]) | GC control |
dofile([filename]) | Execute file, return values |
error(message [, level]) | Raise error |
getmetatable(obj) | Get metatable |
ipairs(t) | Iterator for sequential integer keys |
load(chunk [, chunkname, mode, env]) | Load string as function |
loadfile([filename, mode, env]) | Load file as function |
next(t [, key]) | Next key-value pair |
pairs(t) | Iterator for all key-value pairs |
pcall(f, ...) | Protected call |
print(...) | Print with tostring |
rawequal(a, b) | Compare without metamethods |
rawget(t, key) | Get without metamethods |
rawlen(t) | Length without metamethods |
rawset(t, key, value) | Set without metamethods |
require(modname) | Load module |
select(index, ...) | Select varargs by index |
setmetatable(t, mt) | Set metatable |
tonumber(e [, base]) | Convert to number |
tostring(e) | Convert to string |
type(v) | Type name as string |
warn(msg1, msg2, ...) | Warning messages (5.4) |
_VERSION | "Lua 5.4" |
xpcall(f, msgh, ...) | Protected call with handler |
Notable additions in 5.4
-- warn: emit warnings
warn("this is a warning")
warn("@off") -- suppress warnings
warn("@on") -- resume warnings
-- string.format adds %p for pointers and %I for integers
string.format("%I", 42) -- "42"
string module
Pattern matching
| Pattern | Meaning |
|---|---|
. | Any character |
%a | Letter |
%d | Digit |
%l | Lowercase letter |
%u | Uppercase letter |
%w | Alphanumeric |
%s | Whitespace |
%p | Punctuation |
%c | Control character |
%x | Hex digit |
%z | Zero byte (NUL) |
%A | Non-letter (uppercase = complement) |
[set] | Character set |
[^set] | Negated set |
* | 0 or more (greedy) |
+ | 1 or more (greedy) |
- | 0 or more (lazy) |
? | 0 or 1 |
%bxy | Balanced string (e.g., %b() matches (...) ) |
%n | Capture #n |
Core functions
-- Find
string.find("hello world", "wor") -- 7 9
string.find("hello", "xyz") -- nil
-- Match (returns captures)
string.match("2024-01-15", "(%d+)-(%d+)-(%d+)") -- "2024", "01", "15"
-- Gmatch (iterator)
for word in string.gmatch("hello world", "%S+") do
print(word)
end
-- Gsub (substitute)
string.gsub("hello world", "world", "Lua") -- "hello Lua", 1
string.gsub("aabaa", "a", "x", 2) -- "xxbaa", 2 (limit)
-- Format
string.format("x=%d, y=%.2f", 10, 3.14) -- "x=10, y=3.14"
string.format("hex: %x", 255) -- "hex: ff"
string.format("oct: %o", 255) -- "oct: 377"
-- Sub
string.sub("hello", 2, 4) -- "ell"
string.sub("hello", -2) -- "lo"
-- Byte / Char
string.byte("ABC") -- 65
string.byte("ABC", 2) -- 66
string.char(65, 66, 67) -- "ABC"
-- Rep / Reverse
string.rep("ab", 3) -- "ababab"
string.reverse("hello") -- "olleh"
-- Len
string.len("hello") -- 5
-- Lower / Upper
string.lower("Hello") -- "hello"
string.upper("Hello") -- "HELLO"
Escapes in patterns
%% -- literal %
%. -- literal .
%[ -- literal [
%( -- literal (
%) -- literal )
table module
| Function | Description |
|---|---|
table.concat(t [, sep, i, j]) | Join strings |
table.insert(t, [pos,] value) | Insert element |
table.remove(t [, pos]) | Remove element |
table.move(a1, f, e, t [, a2]) | Move elements between tables |
table.sort(t [, comp]) | Sort in-place |
table.pack(...) | Pack args into table with n field |
table.unpack(t [, i, j]) | Unpack table to values |
table.maxn(t) | Deprecated — don't use |
-- Insert / remove
local t = {1, 2, 3}
table.insert(t, 4) -- {1, 2, 3, 4}
table.insert(t, 2, 99) -- {1, 99, 2, 3, 4}
table.remove(t, 1) -- {99, 2, 3, 4}
-- Sort
table.sort(t, function(a, b) return a > b end) -- descending
-- Move (5.3+)
local src = {1, 2, 3, 4, 5}
local dst = {}
table.move(src, 2, 4, 1, dst) -- dst = {2, 3, 4}
math module
Constants
| Name | Value |
|---|---|
math.pi | 3.14159265358979 |
math.huge | inf |
math.maxinteger | 2^63 - 1 |
math.mininteger | -2^63 |
math.maxinteger + 1 | math.mininteger (wraps) |
Functions
-- Rounding
math.floor(3.7) -- 3
math.ceil(3.2) -- 4
math.modf(3.7) -- 3, 0.7
-- Min/Max
math.min(1, 2, 3) -- 1
math.max(1, 2, 3) -- 3
-- Random
math.random() -- float in [0, 1)
math.random(10) -- integer in [1, 10]
math.random(1, 100) -- integer in [1, 100]
math.randomseed(os.time())
-- Trig
math.sin(math.pi / 2) -- 1.0
math.cos(0) -- 1.0
math.tan(math.pi / 4) -- ~1.0
math.atan2(y, x) -- angle in radians
math.rad(180) -- pi
math.deg(math.pi) -- 180
-- Exponents/Logs
math.exp(1) -- e
math.log(100, 10) -- 2.0
math.sqrt(16) -- 4.0
-- Integer/Float type
math.type(3) -- "integer"
math.type(3.0) -- "float"
-- Integer operations
math.tointeger(3.0) -- 3
math.tointeger(3.5) -- nil
math.ult(1, 2) -- true (unsigned less-than)
io module
-- Simple I/O
io.write("hello\n") -- write to stdout
local line = io.read("l") -- read line from stdin
local all = io.read("a") -- read all stdin
-- File I/O
local f = assert(io.open("file.txt", "r"))
local content = f:read("a")
f:close()
local f = assert(io.open("out.txt", "w"))
f:write("hello\n")
f:close()
-- Read modes
-- "n" number, "l" line (no newline), "L" line (with newline)
-- "a" all, "num" num bytes
-- Popen
local f = io.popen("ls -la", "r")
local output = f:read("a")
f:close()
os module
os.time() -- current timestamp
os.time({year=2024, month=1, day=15}) -- specific time
os.date("*t") -- table: year, month, day, hour, min, sec
os.date("%Y-%m-%d") -- formatted date string
os.difftime(t2, t1) -- difference in seconds
os.execute("ls") -- run shell command
os.exit([code]) -- exit program
os.getenv("HOME") -- environment variable
os.remove("file.txt") -- delete file
os.rename("old", "new") -- rename file
os.tmpname() -- temp file name
os.clock() -- CPU time (high resolution)
debug module
-- Info
debug.getinfo(f [, what]) -- function info table
debug.getlocal(level, index) -- local variable name + value
debug.getupvalue(f, index) -- upvalue name + value
debug.traceback([msg, level]) -- stack traceback string
-- Manipulation
debug.setlocal(level, index, value) -- set local
debug.setupvalue(f, index, value) -- set upvalue
debug.getuservalue(u) -- get userdata value
debug.setuservalue(u, value) -- set userdata value
-- Hooks
debug.sethook(hook, mask [, count])
-- mask: "c" calls, "r" returns, "l" lines
-- Debug mode
debug.debug() -- enter interactive debugger
coroutine module
See Coroutines for full details.
| Function | Description |
|---|---|
coroutine.create(f) | Create coroutine |
coroutine.resume(co, ...) | Resume |
coroutine.yield(...) | Yield |
coroutine.wrap(f) | Wrap as function |
coroutine.status(co) | Status string |
coroutine.isyieldable() | Can yield? |
coroutine.running() | Current coroutine |
coroutine.close(co) | Close coroutine (5.4) |
utf8 module
utf8.len(s) -- codepoint count
utf8.char(65, 20013, 97) -- "A中a"
utf8.codepoint(s [, i, j]) -- codepoints as integers
utf8.codes(s) -- iterator: pos, codepoint
utf8.offset(s, n [, i]) -- byte offset of codepoint n
utf8.charpattern -- pattern for single UTF-8 char
utf8.codepoint("中") -- 20013
-- Iterate UTF-8 string
for pos, cp in utf8.codes("Héllo") do
print(pos, utf8.char(cp))
end
package module
-- Module paths
package.path -- Lua module search path (;-separated)
package.cpath -- C module search path
package.config -- OS-specific config
package.loaded -- table of loaded modules
package.preload -- table of preload functions
package.searchpath(name, path [, sep, rep]) -- find module file
-- Custom loaders
table.insert(package.searchers, my_loader)
require(modname)
Searches package.path for .lua files and package.cpath for .so/.dll files.
local mod = require("mymod") -- loads once, caches
local mod = require("dir.mymod") -- dir/mymod.lua
package.loaded["mymod"] = nil -- force reload