Actors - The unknown feature to make your game run faster

Every game has scripts that increase lag and reduce fps for players, but did you know there is a way to make your scripts run literally faster?

Introducing… ACTORS!

image

According to the Roblox Forum “An Actor is a container for code that can be safely split into its own thread using task.desynchronize(). It should also contain the instances used by its scripts.”

Basically by placing your local or server sided scripts into an actor it gives you the ability to use the functions task.desynchronize() and task.synchronize()

task.desynchronize will (desynchronize) your script allowing it to run on multiple threads, also called parallel programming in lua and other languages

A desynchronized script can run faster because calculations are being split up to multiple threads.

Many programmers use spawn() or coroutine.wrap, but these functions still run one one thread which can be slower than splitting it up using actors.

You’ll want to be careful though, a desynchronized section of a script cannot change many administrator values along with many simple functions like WaitForChild, because of this the desynchronized sections of scripts are often only used for heavy math and calculations that could use the extra speed.

Actor scripts will not be able to influence module scripts!

Often actors are placed within ServerScriptService and ReplicatedFirst.

local function Run()
	local Module = require(game.Workspace.Module)
	local Data = Module.Data
	task.desynchronize()
	--Doing math outside 
	local Calculation = Data * 5 / game.Workspace.CurrentCamera.ViewportSize.X
	task.synchronize()
	if Calculation > 150 then
		game.Workspace.World:Destroy()
	end
	print(Calculation)
end

Run()

game:GetService("RunService").RenderStepped:ConnectParallel(function(DT)
	--ConnectParallel is the same as regular connect but the loop starts in parallel without having to call task.desynchronize()
end)

This is just a random example of how it could be used, but in scripts such as movement handlers that have to do a lot of math with velocity using multiple threads to calculate can save a lot of time when the script is running.

This is just a quick introduction, please check the Roblox Forum on actors and parallel lua for more information on how to use this in your projects!

18 Likes

Thats not really true.

Actors are intended for Scripts, and Instances for those Scripts to work, which do not run in ReplicatedStorage, which will not work at all, if you are just using it to store Instance, you have no use for the Actor at all.


Your use of Parallel Lua here is also not very good, Parallel Lua is meant to heavy work loads that will otherwise cause performance issues, here it will not do much, it is only used for one line of code that in itself does not cause any issues, and its very fast.

7 Likes

Sorry, I meant to write replicated first

Also this tutorial isn’t meant to tell you how to use it, but rather why its worth looking into as a feature not many newer programmers know about.

2 Likes

That seems to be more of a Resource rather than a Tutorial, as Tutorials tell you how to do something.

3 Likes

Hm, that is a good point I can have this taken down

2 Likes

im very new to actors but i found this 100 times more helpful than the official roblox documentation, u explained it really well even if the information u provided isn’t 100%. ppl need posts like this honestly, thanks a million mate i wouldn’t take it down if i were u

4 Likes

To those of you saying this is not a tutorial and the poster should have it taken down, mind you that there are many aspiring programmers out there that otherwise would not have known the use, or at least get a hint of how to use, without this post. Please stop compelling the poster to take this down as it has personally helped me too.

2 Likes

No one really suggested this, @DasKairo was suggesting that this “tutorial” seems more of a resource. The #resources:community-tutorials guide defines a tutorial having a clear, step-by-step instruction, which this topic lacks. And a resource, dumbed down is just explaining in detail

Source: About the Community Tutorials category

A category mismatch can lead to a warning which I’ve gotten before, that is if some unemployed dude flagged your topic.

And if this was a resource, it really needs an explanation on how to use actors efficiently; since parallel luau can have a huge performance overhead if used incorrectly, which just means that it’s worse to use parallel instead of just using normal serial code - and leads to wasted time and effort

TL;DR
i dont see anyone compelling the OP to take it down, except for the OP him/herself. Cairo was just giving valid constructive criticisms

In a nutshell:
improper application of parallel luau = worse performance than normal serial code
resource = book of wisdom
tutorial = a procedure to do/make something

4 Likes