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!
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!