Skip to content
Functions & closuresReference

loadstring

Represents compilation of source text into a function.

Details#

Compiles source text into a callable Luau chunk without executing it. The optional chunkName labels errors and debug information. Check compilation before calling the chunk; compilation errors and errors raised during execution are different stages.

Signature#

Luau
loadstring(source: string, chunkName: string?): (function?, string?)

Parameters#

ParameterTypeDescription
sourcestringLuau source text to compile.
chunkNameoptionalstringOptional label in diagnostics.

Returns#

A compiled function, or nil and a compilation error message.

Examples#

Example

Luau
local chunk, err = loadstring("return 6 * 7", "example")
if chunk then
    print(chunk()) -- 42
else
    warn(err)
end