Schedule a Luau snippet to run in the background (fire-and-forget)
run-deferredSchedules a Luau snippet on its own thread and returns immediately without waiting, so you can run background tasks like watchers, auto-farms, pollers, or delayed code while issuing other calls.
Instructions
Schedule a Luau snippet to run on its OWN thread and return immediately WITHOUT waiting for it to finish. Use this to start something that runs in the background while you keep issuing other tool calls — e.g. kick off a long watcher loop, an auto-farm, a background poller, or any snippet you want to run later or off the current thread so it cannot block the round trip. The code is COMPILED FIRST via loadstring; a syntax error is returned cleanly as { error } and nothing is scheduled. On success the compiled function is handed to the chosen scheduler and the tool returns at once. Modes (Luau task library): - 'spawn' -> task.spawn(fn): start running on a fresh thread immediately. - 'defer' -> task.defer(fn): run on a fresh thread at the end of the current resumption cycle. - 'delay' -> task.delay(delaySec, fn): run on a fresh thread after delaySec seconds. IMPORTANT: because this does not wait, you will NOT see the snippet's return value, errors, or print output here — the scheduled thread runs independently and any error inside it is swallowed by task. If the snippet installs state or a loop, YOU are responsible for stopping it (e.g. have it watch a getgenv() flag). Requires loadstring and the task library (both guarded). Returns { scheduled = true, mode, delaySec? } or { error }. Signature: { code: string, mode: any?, delaySec: any?, threadContext: number? }. Phase: act; cost=medium; idempotency=contextual-write. Requires: active-client, explicit-mutation-approval, validated-source. Produces: operation-receipt. Verify with: assert-state. Safety: MUTATING; executes caller-selected behavior in the live client. On failure: inspect tool-schema for exact fields, defaults, constraints, and an invocation example.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The Luau snippet to run in the background. Compiled with loadstring, then scheduled on its own thread. Since nothing is awaited, build in your own stop condition for long loops (e.g. `while getgenv().__myFlag do ... task.wait() end`) so you can shut it down later. | |
| mode | No | How to schedule the thread: 'spawn' starts it immediately on a new thread; 'defer' runs it at the end of the current resumption cycle; 'delay' runs it after `delaySec` seconds. Default 'spawn'. | spawn |
| delaySec | No | Seconds to wait before the snippet runs. Only used when mode='delay' (ignored for spawn/defer). Default 0. | |
| threadContext | No | Optional Roblox thread identity for this call; omit it to use the server default. |