Codectionary / Developer documentation / Lua

Lua text chunks with load

Lua 5.2+ load compiles text with an optional environment. Compilation failure returns nil and an error; running the resulting function is a separate operation.

Syntax

local chunk, err = load(source, chunkName, "t", environment)

Examples

Lua text chunks with load

Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.

local chunk, err = load("return price * quantity", "invoice", "t", {price = 7, quantity = 3})
assert(chunk, err)
local ok, total = pcall(chunk)
assert(ok, total)
print(total)
  1. Output: 21. Mode t permits text only, and the environment supplies the free names price and quantity.

Best practices

  • Use trusted code unless the host provides a hardened sandbox and resource limits. Do not assume standard Lua load exists in Roblox.

Runtime & versions

Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Lua reference manuals

Specifications & further reading