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

# loadfile

Reads the file at `path`, compiles it as Luau, and returns the resulting chunk as a function. The function is not called yet - invoke it yourself when you're ready to run the code.

```luau theme={null}
loadfile(path: string, chunkname: string?): function?, string?
```

`path` is relative to Seliware's workspace folder.

## Parameters

| Parameter   | Type      | Description                                                                                |
| ----------- | --------- | ------------------------------------------------------------------------------------------ |
| `path`      | `string`  | Luau source file to load.                                                                  |
| `chunkname` | `string?` | Optional label that appears in stack traces and error messages. Defaults to the file path. |

## Returns

`(function, nil)` on success, `(nil, string)` on failure. Always check the second return before invoking the first.

| Outcome                      | First return      | Second return |
| ---------------------------- | ----------------- | ------------- |
| File compiled cleanly        | callable function | `nil`         |
| File missing or syntax error | `nil`             | error message |

## Example

Cache a helper function via `loadfile()` so you can re-call it without re-reading the file:

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  local fn = loadfile("script.luau")
  =======
  -- loadfile(path: string, chunkname: string?): function?, string?
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  local helper, err = loadfile("meow/aimbot.luau", "aimbot")
  if not helper then
      warn(`couldn't load helper: {err}`)
      return
  end

  helper()
  -- ...
  ```
</CodeGroup>

<Tip>
  Compiles the script but does not execute it until the returned function is called.
</Tip>
