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

# Globals

> Read the script's identity, wait with rb.run, rb.sleep, rb.after and rb.async, and load modules.

These are the names on `rb` itself rather than in a namespace, and `require`.
A script reaches for them to read its own identity, to wait without blocking
other scripts, and to split its code across files.

**Needs:** a coroutine for `rb.sleep`, so call it inside `rb.run`, `rb.after`
or `rb.async` (see [Where code runs](#where-code-runs)).

This script prints its name and platform, counts three steps a second apart,
and prints a line after five seconds:

```luau theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
-- rebind: min_sdk=3.9.0 name=example version=1.0.0
print(rb.info.name .. " on " .. rb.info.platform)

rb.run(function()
  for i = 1, 3 do
    print("step " .. i)
    rb.sleep(1000)
  end
end)

rb.after(5000, function()
  print("five seconds in")
end)
```

## rb.info

`rb.info: RebindInfo`, a read-only table available from the first line of the
script.

| Field | Type | Value |
| - | - | - |
| `name` | string | the modeline `name`, or `Untitled` |
| `version` | string | the installed Rebind SDK version, not the script's own `version` |
| `min_sdk` | string | the declared `min_sdk` |
| `tick_rate` | number | the modeline `tick_rate` |
| `runtime_uuid` | string | the id of this running instance |
| `instance` | string | `"replace"`, `"single"` or `"multiple"`; see [`instance`](https://docs.rebind.gg/reference/sdk/modeline) |
| `platform` | string | `"windows"` or `"macos"` |

## rb.run

`rb.run(fn: () -> ()) -> TaskHandle`

Runs `fn` as a coroutine and returns at once. The coroutine starts on the next
engine tick. Inside it, `rb.sleep` and every other coroutine-only call work.

An uncaught error inside the coroutine stops the whole script with that error and
its traceback in the script's **Logs** (see [Where code runs](#where-code-runs)).
The returned [TaskHandle](https://docs.rebind.gg/reference/sdk/handles.md) stops the coroutine.

## rb.sleep

`rb.sleep(ms: number) -> ()`

Pauses the current coroutine for `ms` milliseconds; fractions are allowed.
Called outside a coroutine it raises
`Sleep() can only be called inside a Run() coroutine`.

## rb.after

`rb.after(ms: number, fn: () -> ()) -> TaskHandle`

Runs `fn` once after `ms` milliseconds. It is `rb.run` around `rb.sleep(ms)`
then `fn()`, so the same error rule applies.

## rb.async

`rb.async(fn: (...any) -> ...any) -> (...any) -> ...any`

Returns a function that runs `fn` in a coroutine. Called from outside a
coroutine, the wrapper passes its arguments to `fn` inside a new `rb.run` and
returns `false`. Called from inside a coroutine, it calls `fn` directly and
returns `fn`'s results. Use it for `rb.bind` callbacks that need `rb.sleep`.
Do not pass it straight to `rb.on("down", ...)`: its `false` would block every
key.

This script holds W for half a second on F10:

```luau theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
-- rebind: min_sdk=3.9.0 name=example version=1.0.0
rb.bind("F10", rb.async(function()
  rb.hid.down("W")
  rb.sleep(500)
  rb.hid.up("W")
end))
```

## require

`require(module: string) -> any`

Loads a module file and returns its value. A module is a file that `return`s a
value, usually a table. Each resolved file is evaluated once; later calls return
the cached value.

Resolution, relative to the script's own directory, or to the package root for
a script inside a package:

* A leading `./` is stripped and dots are path separators, so
  `require("lib.math")` looks for `lib/math`.
* Without an extension the candidates are `<name>.luau`, `<name>.lua`,
  `<name>/init.luau`, then `<name>/init.lua`. An explicit `.luau` or `.lua`
  suffix names that file only.
* A name that directory cannot resolve is tried again in the `packages`
  folder of Rebind's app directory, and a name starting with `macros/` is tried
  in the `macros` folder of your scripts, where the recorder saves takes
  (`macros.` works the same way).
* `require("@rebind/pro")` returns `rb`, for `rb.ai` and `rb.phone` (see
  [Rebind Pro](https://docs.rebind.gg/account/pro.md)).

Errors: a name containing `..`, or a file that resolves outside the directory,
raises `require: path traversal not allowed: <name>`; a name nothing matches
raises `module '<name>' not found (searched in <dir>)`.

A `require`d module needs no modeline of its own. See
[Splitting across files](https://docs.rebind.gg/concepts/scripting-guide.md#splitting-across-files-require).

## Where code runs

A script runs on the engine thread, which it shares with every other script and
with input dispatch. Only `rb.run`, `rb.after`, and an `rb.async` wrapper called
outside a coroutine start a coroutine. Everything else is a plain call, so a
coroutine-only call such as `rb.sleep`, `rb.hid.press`, `rb.net.get` or
`rb.screen.capture` raises there. To wait, start a coroutine with `rb.run` from
inside the callback.

| Code | In a coroutine | An uncaught error |
| - | - | - |
| top-level code, `start`, `focus`, `blur`, `tick` | no | stops the script |
| `down`, `up`, `move`, `scroll`, `axis` and observer handlers | no | logged; the event continues |
| `rb.bind` callback, `when`, `release` | no | as an input handler |
| `rb.timer` callbacks | no | logged to the script's **Logs**; the script keeps running |
| `rb.net.listen` handler | no | stops the script |
| `rb.net.udp_listen`, `ws_listen`, `ws_connect` handlers | no | logged to the script's **Logs**; the server keeps running |
| the body of `rb.run`, `rb.after`, `rb.async` | yes | stops the script |

Wrap work that may fail in `pcall` and report it with `rb.log.error`, so the
message reaches the script's **Logs**.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.