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

# loadstring

Takes a string of Luau source code, compiles it, and returns the compiled chunk as a function. Compiling doesn't run the code - call the returned function to do that.

```luau theme={null}
loadstring(source: string, chunkname: string?): function?, string?
```

Most commonly used to run a script hub fetched over HTTP in one line: `loadstring(game:HttpGet(url))()`.

## Parameters

| Parameter   | Type      | Description                                                                                     |
| ----------- | --------- | ----------------------------------------------------------------------------------------------- |
| `source`    | `string`  | The Luau source code to compile.                                                                |
| `chunkname` | `string?` | Optional label that appears in stack traces and error messages. Defaults to a generated string. |

## Returns

`(function, nil)` on success, `(nil, string)` on failure.

| Outcome                 | First return      | Second return |
| ----------------------- | ----------------- | ------------- |
| Source compiled cleanly | callable function | `nil`         |
| Syntax / compile error  | `nil`             | error message |

## Example

Fetch a script hub over HTTP and run it through `loadstring()`, guarding the compile so a syntax error in the fetched source warns you instead of failing silently:

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  local fn = loadstring("print('hello')")
  =======
  -- loadstring(source: string, chunkname: string?): function?, string?
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  local source = game:HttpGet("https://example.com/hub.lua")

  local chunk, err = loadstring(source, "hub")
  if not chunk then
      warn(`something went wrong w/ the script hub: {err}`)
      return
  end

  chunk()
  ```
</CodeGroup>

<Tip>
  Does not execute bytecode; pass plain text Luau code.
</Tip>
