Sandbox
This page lists what a script's Lua environment contains on each side: the Lua version, the standard libraries that are available, the changes made to some standard functions, and the limits scripts run under. Client scripts run code the server sent on the players' computers, so they get a restricted environment; server scripts are chosen by the server owner and get more.
Available on: Server, Client
Lua version
Scripts run on LuaJIT 2.1, which implements Lua 5.1 with a few extensions. If you know Lua 5.3 or 5.4, note the differences:
- There is no integer type: every number is a 64-bit float. Integers are exact up to 2^53.
- No
//integer division (usemath.floor(a / b)), no&,|,~,<<,>>operators (use thebitlibrary instead), and noutf8library. unpackis a global function (there is notable.unpackortable.pack), and there is nomath.tointeger.table.moveis available.gotoand labels work, as doesloadwith a string.- LuaJIT's
bitlibrary is loaded on both sides for bitwise operations (see bit).
Script isolation
Every .lua file of a resource runs in its own Lua state (see Resources). Globals are not shared between files or resources. Files talk to each other through events and share code through require.
Libraries per side
| Library or global | Server | Client |
|---|---|---|
base functions (pairs, ipairs, type, tostring, tonumber, pcall, xpcall, error, assert, select, rawget, setmetatable, getfenv, setfenv, collectgarbage...) | yes | yes |
coroutine | yes | yes |
string | yes | yes, without string.dump |
table | yes | yes |
math | yes | yes |
os | only os.time and os.date | only os.time and os.date |
load, loadstring | source text only | source text only |
loadfile, dofile | source text only | not available |
require | standard Lua require with the resources folder added | a restricted require that only loads resource files |
package | yes (including package.loadlib and C modules) | not available |
jit | yes | only jit.on, jit.off, jit.flush, jit.status, jit.version, jit.version_num, jit.os, jit.arch |
ffi | yes | not available |
io | not available | not available |
debug | not available | not available |
bit | yes | yes |
Server scripts can reach native code through ffi and package.loadlib, so only run server resources you trust, the same as any program on the server machine.
bit
The bit library is LuaJIT's bitwise operations library, loaded on both sides. Its functions work on 32-bit integers: the arguments are converted to 32-bit integers and the results are signed 32-bit numbers.
| Function | Result |
|---|---|
bit.band(a, b, ...) | bitwise AND |
bit.bor(a, b, ...) | bitwise OR |
bit.bxor(a, b, ...) | bitwise XOR |
bit.bnot(a) | bitwise NOT |
bit.lshift(a, n) | a shifted left by n bits |
bit.rshift(a, n) | a shifted right by n bits, filling with zeros |
bit.arshift(a, n) | a shifted right by n bits, keeping the sign bit |
bit.rol(a, n), bit.ror(a, n) | a rotated left or right by n bits |
bit.bswap(a) | a with its four bytes in reverse order |
bit.tobit(a) | a converted to a signed 32-bit number |
bit.tohex(a, n) | a as a hexadecimal string of n digits (8 by default, upper case when n is negative) |
local flags = bit.bor(AclLevel.Admin, AclLevel.Moderator)
print(flags) -- 6
print(bit.band(flags, AclLevel.Admin) ~= 0) -- true
print(bit.lshift(1, 4), bit.tohex(255, 2)) -- 16 ff