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#
loadstring(source: string, chunkName: string?): (function?, string?)Parameters#
| Parameter | Type | Description |
|---|---|---|
source | string | Luau source text to compile. |
chunkNameoptional | string | Optional label in diagnostics. |
Returns#
A compiled function, or nil and a compilation error message.
Examples#
Example
local chunk, err = loadstring("return 6 * 7", "example")
if chunk then
print(chunk()) -- 42
else
warn(err)
end