Guide: Actors & parallel Luau
Running code inside a game's actors and sending messages back out.
Roblox runs Actor instances in their own Luau states so their scripts can run in parallel. Those states are separate, so a value in one can't be seen from another, and your script starts outside all of them.
That's kind of the whole problem. More and more games put anti-cheat and game logic inside actors, so if you want to see or change it you have to get your code *into* the actor instead of calling from outside.
for _, actor in ipairs(getactors()) do
print(actor:GetFullName())
for _, thread in ipairs(getactorthreads(actor)) do
print(" thread", thread)
end
end
print("currently inside an actor?", isparallel())
print("current actor:", get_current_actor())run_on_actor compiles and runs source code inside the actor. You pass source, not a function, because a function can't cross over. That separation is exactly what you're working around.
local actor = getactors()[1]
run_on_actor(actor, [[
-- This runs inside the actor's own state.
print("hello from", get_current_actor())
print("parallel:", isparallel())
]])Upvalues don't cross over
The source string is all the actor gets. Anything the code needs has to be in it or come through a channel. Locals from your script don't exist over there.
create_comm_channel opens a pipe (backed by a BindableEvent) with an id you can pass into the actor as text. The actor grabs it with get_comm_channel and then both sides have a normal signal to talk through.
local id, channel = create_comm_channel()
channel.Event:Connect(function(...)
print("from the actor:", ...)
end)
run_on_actor(getactors()[1], ([[
local channel = get_comm_channel(%d)
channel:Fire("ready", tick())
]]):format(id))on_actor_state_created fires whenever a new actor state shows up, *before* it runs any queued code. Use it if you need to be in an actor from the very start.
on_actor_state_created:Connect(function(actor)
run_on_actor(actor, [[ getgenv().__seen = true ]])
end)The Actors chapter has every function. Read Environment too, since getgenv, getrenv and gettenv act differently depending on which state you call them from.