Syntax
loadstring(source: string, chunkName?: string): function | nil | string | nilArguments
| Name | Type | Description |
|---|---|---|
source | string | Luau source string to compile. Embedded null bytes after the first null are not preserved. |
chunkName? | string | Optional chunk name used for error reporting. Defaults to "@" when omitted or nil. |
Returns
| Name | Type | Description |
|---|---|---|
chunk | function | nil | Loaded function when compilation and loading succeed, otherwise nil. |
errorMessage | string | nil | Load error message when loading fails. Not returned on success. |
Description
Compiles a Luau source string and returns the loaded function. If loading fails, it returns nil and an error message. The returned function is not executed automatically.
Call it with 2 parameter(s): source, chunkName. The argument table explains which values are required and which ones only refine the behavior.
It returns chunk (function | nil), errorMessage (string | nil). Use the returns table to separate successful values from nil results and recoverable errors.
Example
Compile source text and handle a possible compiler error.
local chunk, message = loadstring("return 6 * 7", "ExampleChunk")
if chunk then
print(chunk())
else
warn(message)
end