How scripts run
This page explains the engine under your scripts: when your code runs, how waiting works, what events fire, and what happens when something breaks.
One engine, many scripts
All scripts share one Luau engine running on its own thread, apart from the game and the menu. Each script gets its own set of globals, so two scripts can both have a variable called esp without clashing.
Two tables are shared on purpose. _G and shared are the same table in every script, so you can pass values between scripts:
-- in one script
shared.lastTarget = "Builderman"
-- in another script
print(shared.lastTarget)The tick rate
The engine steps in ticks, 240 of them a second, so one tick is about 4 milliseconds. The rate is fixed.
The tick rate decides how precise waits are and how often Heartbeat fires. 240 is plenty for drawing and reading the game.
Waiting
task.wait(seconds) pauses the current thread and lets everything else run. It returns how long it actually waited. With no argument it waits one tick.
local waited = task.wait(0.5)
print("waited", waited)
task.wait() -- one tickwait(seconds) is the same thing under its old name. Both return the elapsed time, so the classic while wait() do loop works.
Running things side by side
A script can run several threads at once. Each one runs until it waits, then the next gets a turn.
| Function | What it does |
|---|---|
task.spawn(fn, ...) |
Runs fn right away on a new thread, with the extra arguments. Returns the thread. |
task.defer(fn, ...) |
Runs fn on a new thread a moment later, in the same tick. |
task.delay(seconds, fn, ...) |
Runs fn after seconds. |
task.cancel(thread) |
Stops a thread that is waiting or deferred. |
spawn(fn) |
The same as task.spawn. |
local ticker = task.spawn(function(label)
while true do
print(label)
task.wait(1)
end
end, "tick")
task.delay(5, function()
task.cancel(ticker)
print("ticker cancelled")
end)RunService events
RunService gives you three events that fire over and over:
| Event | When it fires | Argument |
|---|---|---|
RenderStepped |
Once per tick in which evasive drew at least one frame | Seconds of frames since the last one |
Heartbeat |
Every tick | Seconds since the last tick |
Stepped |
Every tick, like Heartbeat | Seconds since the last tick (one argument only) |
Use RenderStepped for anything you draw, since it lines up with the overlay's frames. Use Heartbeat for work that has nothing to do with drawing.
local RunService = game:GetService("RunService")
local frames = 0
local connection = RunService.RenderStepped:Connect(function(dt)
frames += 1
end)
task.wait(5)
connection:Disconnect()
print("frames in 5 seconds:", frames)Connect returns a connection. connection:Disconnect() stops it, and connection.Connected tells you whether it is still live. signal:Once(fn) connects for a single call, and signal:Wait() pauses until the next time the event fires.
Every callback runs on its own thread, so a callback can call task.wait without holding up the others.
The other events you can connect to are Players.PlayerAdded, Players.PlayerRemoving, Player.CharacterAdded, Player.CharacterRemoving, UserInputService.InputBegan and UserInputService.InputEnded.
Errors
When code fails, the console gets a red line with the file and line number, then a grey "stack" line showing how it got there:
local character = nil
print(character.Name) -- hello.lua:2: attempt to index nil with 'Name'An error ends the thread it happened in. It does not take down the rest of the script. If the script has other threads, connections or drawings, those keep going.
To catch an error yourself, wrap the risky part in pcall:
local ok, result = pcall(function()
return workspace:FindFirstChild("Map"):FindFirstChild("Spawn").Position
end)
if ok then
print("spawn at", result)
else
warn("no spawn here:", result)
endOne thing works differently from Roblox here. error("message") prints a red line and carries on. It does not raise. To raise an error that pcall can catch, use assert:
local ok, message = pcall(function()
assert(false, "something went wrong")
end)
print(ok, message)warn prints a yellow line. print prints a grey one.
The time budget
The engine runs one thread at a time. A thread that never waits would block every other script, so each thread gets 3 seconds of running time before it has to yield.
If a thread goes over, evasive stops the whole script and prints "script exceeded 3000 ms without yielding". pcall cannot catch this.
The fix is always the same: put a task.wait() in long loops.
local count = 0
for i, d in ipairs(workspace:GetDescendants()) do
if d:IsA("BasePart") then
count += 1
end
if i % 2000 == 0 then
task.wait() -- give the engine a breath on huge maps
end
end
print(count, "parts")When a script stops
A script keeps running as long as it has something to do. That means a thread that is waiting, an event connection, a drawing on screen, a menu tab or code queued for a teleport.
It stops when:
- you press its stop button, Stop all, or run it again (that restarts it);
- its code has finished and nothing is left (the console says "finished");
- a thread goes over the time budget;
- evasive closes.
When it stops, evasive removes its drawings and its menu tab, drops its connections, cancels its web requests and releases any keys it held.
Teleports
Scripts keep running when you teleport to another place. game and workspace always point at the place you are in now. Objects you saved in variables before the teleport belong to the old place and read as empty (no name, no parent, no children), so look them up again after a teleport.
To run code once the next place has loaded, queue it:
queue_on_teleport([[
print("arrived in", game.PlaceId)
]])The queued code runs as part of the script that queued it, and that script stays running until then. A syntax error in the queued code shows up right away, not after the teleport.
The menu
Scripts run the same with the menu open or closed. Drawings are painted under the menu, so they never cover it. Input that a script sends to Roblox is held back while the menu is open (see Input and keybinds).
No place loaded
When Roblox is on its home screen there is no game to read. Touching game or workspace then raises "game is not available: Roblox has no place loaded". An autoexec script only starts once a place has loaded, so it never sees this.