> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# loadstring

> 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.

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.

## Syntax

```lua theme={null}
loadstring(source: string, chunkName?: string): function | nil | string | nil
```

## Arguments

| Name        | Type     | Required | Description                                                                                |
| ----------- | -------- | -------- | ------------------------------------------------------------------------------------------ |
| `source`    | `string` | Yes      | Luau source string to compile. Embedded null bytes after the first null are not preserved. |
| `chunkName` | `string` | No       | 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.      |

## Example

Compile source text and handle a possible compiler error.

```lua theme={null}
local chunk, message = loadstring("return 6 * 7", "ExampleChunk")

if chunk then
    print(chunk())
else
    warn(message)
end
```
