ReferenceClosures

loadstring

Compiles Luau source into a function you can call.

Syntax

loadstring(source: string, chunkname: string?) -> (function?, string?)

Also available as

load_string

Parameters

ParameterTypeDescription
sourcestringLuau source code to compile.
chunknamestring?Name used in error messages and tracebacks. Defaults to Apex's unnamed chunk label.

Returns

TypeDescription
function?The compiled chunk, ready to call, or nil when the source does not compile.
string?The compiler error when compiling failed.

Description

The returned function runs in your executor environment, so it sees getgenv values and has the same identity as the script that loaded it. Compiling does not run anything; call the result to run the code. A syntax error does not raise: you get nil and the message, so check the first result before calling it.

Apex uses the compiler selected by the signed engine profile. The result must be compatible with that engine; this does not promise byte-for-byte equality with chunks produced using different compiler options.

Example

local chunk, problem = loadstring("return 2 + 3")
print(chunk())

local broken, message = loadstring("return +", "broken")
print(broken, message)

loadstring(game:HttpGet("https://example.com/script.luau"))()

Notes

These are Game executor contracts. Check build requirements and session lifetimes before using them.