Parallel Luau: Actors and multithreading

Learn Parallel Luau for Roblox. Put scripts under Actors, desynchronize for independent work, synchronize to touch the world, share data with messages and SharedTable, and judge when Beacon Yard would benefit.

Concept lesson, about 65 minutes, 10 steps

By the end you can

  • Explain why task.spawn does not use more than one CPU core, and what Actors change
  • Split work into a parallel phase (task.desynchronize) and a serial phase (task.synchronize)
  • Check an API's thread safety before calling it in parallel, and pass data with Actor messages or SharedTable
  • Decide when parallelism helps (independent math and raycasts) and when it does not (one drone, ComputeAsync)

Before this: Events, signals and the task library, Performance: MicroProfiler, memory, network

Step 1 of 10

Eight cores, one busy

Your phone probably has six or eight CPU cores. By default, every Luau script in your game runs on one of them, one script at a time. task.spawn does not change that: it hands a job to a helper, and the helpers take turns on the same core.

Both sidesRemember: task.spawn is hand a job to a helper.

Before pressing Run, predict which string prints first: caller continues or helper starts?

Luau with roblox-lite (not Roblox)
Ctrl Enter runs the cell. Checked against the expected output

The helper ran all the way through before the caller continued. Nothing happened at the same time. If the helper had done 30 ms of maths, the caller and every other script would have waited 30 ms.

Choose one

You move DroneService's per-frame step into task.spawn, and say it costs 30 ms. What happens to the server frame?

You move DroneService's per-frame step into task.spawn, and say it costs 30 ms. What happens to the server frame?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 2 of 10

Where the helper picture breaks

Where does it break?“Hand a job to a helper” is a good picture of task.spawn. Where does it stop being true?

It breaks here. Helpers take turns rather than working at the same time: a spawned function that never yields blocks the caller and every other script.

The true model. task.spawn creates a coroutine and resumes it right away on the same scheduler. Concurrency is cooperative: work interleaves only at yields such as task.wait.

Parallel Luau is the case where helpers really do work at the same time. To get it, you put scripts under ActorActorAn instance whose descendant scripts can run in parallel with scripts under other Actors.Think of it as a separate workbench: one worker per bench, many benches working at once. instances. An Actor is a unit of isolation: scripts inside one Actor run one after another, and scripts in different Actors can run on different cores at the same moment1.

Choose one

You put all eight crawler NPC scripts under one Actor and call task.desynchronize in each. How many run at the same time?

You put all eight crawler NPC scripts under one Actor and call task.desynchronize in each. How many run at the same time?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 3 of 10

Parallel phase, serial phase

Being under an Actor is not enough. Code still runs serially until it asks to go parallel with task.desynchronize(), and it must come back with task.synchronize() before it changes the world2. A signal can start in parallel directly with ConnectParallel1.

luau
-- A script under an Actor
local RunService = game:GetService("RunService")
 
RunService.Heartbeat:ConnectParallel(function(dt)
	-- PARALLEL: read the world, do maths, cast rays
	local target = chooseTarget() -- pure maths and raycasts
 
	task.synchronize()
	-- SERIAL: write to instances
	model:PivotTo(target)
end)

The engine runs every Actor's parallel part together, then every serial part one at a time. Most real systems need both: compute in parallel, apply in serial.

Go deeper Why writes must wait

If two Actors moved the same part at the same moment, which position should win, and what would a third Actor reading it in between see? The engine avoids the question by letting parallel code read freely and making every write happen in the serial phase, one Actor at a time. You pay for that safety with a switch back to serial, so batch your writes.

Put in order

Imagine CombatService.trySwing's hit check moved into its own Actor per player. Put one Actor's work in order.

  1. Call task.synchronize to return to the serial phase
  2. require the modules the script needs, in serial
  3. Write the results to instances
  4. Call task.desynchronize to enter the parallel phase
  5. Do the independent maths and raycasts

Link to this step · This confused me

Step 4 of 10

Thread safety: read the label

In the parallel phase the engine checks every API call against its thread safety level and blocks unsafe ones1:

LevelPropertiesFunctions
UnsafeCannot be read or written in parallelCannot be called
Read ParallelCan be read, not writtenn/a
Local SafeRead and write inside the same Actor; read-only from other ActorsCallable inside the same Actor
SafeRead and writeCallable

Every API reference page prints the level. Two examples that matter for games: WorldRoot:Raycast is Safe3. Path:ComputeAsync, which computes a pathfinding route, is Unsafe4. An API with no label is Unsafe.

Choose all that apply

Which of these may run after task.desynchronize?

Which of these may run after task.desynchronize?

Press 1 to 5 to choose, Enter to check.

Link to this step · This confused me

Step 5 of 10

Talking between Actors

Real parallel helpers sit at separate workbenches. They cannot reach into each other's drawers. They pass notes, or they share one locked board on the wall that only one of them can write on at a time.

Think of it as Hand a job to a helper

Actors cannot just share Luau tables, because two cores could change the same table at once. The engine gives you three channels1:

  • Actor messages. actor:SendMessage(topic, ...) sends data to one Actor; a script inside binds a handler with BindToMessage or BindToMessageParallel5. Sending does not wait for the receiver.
  • SharedTable. A table-like value many Actors can read and update atomically. Keys are strings or small non-negative integers; values are booleans, numbers, vectors, strings, nested SharedTables or serialisable data types6. You find one by name in SharedTableRegistry or pass it in a message.
  • The data model. Actors can read properties and attributes others wrote, but writes need the serial phase, so this forces frequent synchronising.
luau
-- Coordinator (serial): hand chunk jobs to workers
for i, worker in workers do
	worker:SendMessage("GenerateChunk", i, seed)
end
 
-- Worker script under each Actor
local actor = script:GetActor()
actor:BindToMessageParallel("GenerateChunk", function(index, seed)
	local voxels = generate(index, seed) -- parallel maths
	task.synchronize()
	writeChunk(index, voxels) -- serial write
end)
Match the pairs

Match each need to the channel that fits.

Link to this step · This confused me

Step 6 of 10

When parallelism helps, and when it does not

Parallel Luau helps when you have many independent jobs of real maths: validating dozens of hits with raycasts, scoring targets for many NPCs, generating terrain chunks from noise1. It does not help with one small job, with work that mostly writes instances, or with APIs that are Unsafe.

Look at Beacon Yard honestly. The server runs one drone, and its step is a few vector operations and a PivotTo write. The MicroProfiler capture shows where the frame goes:

Roblox Studio
The MicroProfiler paused over the Beacon Yard viewport: frame bars along the top and a timeline of tasks below.

Select a numbered pin, or a row below, to read what it marks.

A capture of the yard during a drill. There is no batch of independent jobs here for workers to share.

The m08-crawler milestone adds a ground enemy with pathfinding, which changes the picture a little. ComputeAsync itself must stay serial. What can go parallel is everything around it: line-of-sight raycasts, choosing which crate to go around, steering between waypoints, for several crawlers at once.

Choose one

Which change is most likely to gain from Parallel Luau?

Which change is most likely to gain from Parallel Luau?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 7 of 10

Split the work fairly

When you do have a batch, split it evenly. The docs recommend more Actors than cores: smaller pieces balance better across the cores the device actually has1. Each Actor should still do enough work to be worth the switch. Predict how split(10, 4) divides 10 jobs across 4 Actors before pressing Run.

Luau
Ctrl Enter runs the cell. Checked against the expected output
Predict the output

Using split from the cell above, what does split(7, 3) return?

print(table.concat(split(7, 3), ", "))
Using split from the cell above, what does split(7, 3) return?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 8 of 10

What professionals do

Other ways pros do this

OptionWhat it isWhen pros pick itTrade-offs
Parallel Luau with ActorsScripts under Actors that desynchronize for independent work.Many independent, maths-heavy jobs: hit validation, NPC sensing, procedural chunks.Serial writes, thread-safety rules, harder debugging. No gain for small work.
Spread work over framesProcess N items per frame with task.wait or a queue.Work that is not urgent: rebuilding a leaderboard, regenerating distant chunks.Still one core; results arrive later.
Do less workCache results, lower the rate, cull what players cannot see.Always first. Most slow code does work nobody needs.Needs a profile to know what is unneeded.
Move work to the clientLet each client compute cosmetic work for itself.Visual-only effects such as particles, idle animations, camera.Never for anything that decides gameplay; clients can lie.
Put in order

A server frame runs long when 20 crawlers chase players. Order the professional's steps.

  1. Capture again on the same scene and compare
  2. Move independent sensing raycasts for each crawler into its own Actor
  3. Remove work nobody needs, such as re-pathing crawlers that are far from every player
  4. Capture a MicroProfiler dump during the chase

Link to this step · This confused me

Step 9 of 10

Challenges

Level 1, Apprentice

Challenge

Classify three APIs from the game by their thread safety.

Start from
The game at milestone m06 (download)

Done means

  • You look up the thread safety line for three APIs Beacon Yard uses
  • You say for each whether it could run after task.desynchronize

Meet the criteria above, then record your own result. No test grades this check.

Choose one

An API reference entry gives no thread safety level. What should you assume?

An API reference entry gives no thread safety level. What should you assume?

Press 1 to 4 to choose, Enter to check.

Level 2, Builder

Challenge

In a separate test place, measure whether parallel raycasts help on your machine.

Start from
The game at milestone m06 (download)

Done means

  • A test place has 8 Actors, each casting 200 rays per Heartbeat in parallel
  • The rays are cast after ConnectParallel or task.desynchronize, and nothing is written in parallel
  • You compare a MicroProfiler capture with the same rays cast serially by one script

Meet the criteria above, then record your own result. No test grades this check.

Level 3, Professional

Challenge

Fix the planning helpers in challenges/parallel_luau_work.luau on m14-ship. Press Play and run this in the server command bar:

luau
game.ServerScriptService.RunGameTests:Invoke("parallel-luau")
Start from
The game at milestone m14 (download)
Grade it
In Studio, press Play, then run game.ServerScriptService.RunGameTests:Invoke("parallel-luau") in the server command bar. Copy the pass code from Output.

Done means

  • phaseFor returns parallel only for Raycast and math.noise, and serial for everything else
  • splitIntoBatches keeps every job and no two batches differ by more than one
  • challenges/parallel-luau.luau prints TEST PASS for all nine cases

The Course Runner prints this code when the test passes.

Link to this step · This confused me

Step 10 of 10

Recap

Retiring the analogy

You no longer need “hand a job to a helper”. task.spawn creates a coroutine and resumes it right away on the same scheduler. Concurrency is cooperative: work interleaves only at yields such as task.wait.

Recap

  1. task.spawn takes turns on one thread. Real parallelism needs scripts under different Actors.
  2. Desynchronize for reads, maths and Safe calls; synchronize before any write. Check every API's thread safety.
  3. Use it for many independent jobs, after profiling. ComputeAsync stays serial; one drone gains nothing.

task.spawn is hand a job to a helper, until helpers take turns rather than working at the same time: a spawned function that never yields blocks the caller and every other script.

Next Mobile: streaming, memory and draw budgets

Link to this step · This confused me