Hopefully, this first part is a somewhat quick question to answer. Shockingly, I cannot find documentation anywhere that explains this. There’s a second, more lengthy explanation and issue below the main question of my topic.
So, it is stated that a ServerScript needs to have an Actor above it somewhere in order to get thrown into parallel when you run task.desynchronize(). All scripts together underneath the Actor will run in series with eachother. This is easily understood.
However, do all of the scripts need to be direct children of the Actor, or can they be deeper inside the rig somewhere?
“Deeper” example (this made zero difference on performance of the CalculateSpeeds script, read below):
Direct child example (wouldn’t be an ideal solution if this is required, but I am willing to try working with it if necessary):
If being a direct child is required, it will add extra steps within the script itself as you will have to put them in specific places, meaning you can’t just “script.Parent” a part, you’d have to add extra text to specifically find said part.
I should mention why I ask this question, and maybe it is a more lengthy answer this time.
I have a game that includes multiple automatic trains. These trains run on the server (they force the Network Owner to the server) and control their speed, stops, etc. all on their own. The trains work based on sensors along the track, so they only have to run code when a trigger on the train touches a sensor.
The most intensive part of the code is a loop that gradually slows down (or speeds up) the train, though the loop only activates and runs when the speed needs to be changed. This loop simply sets a NumberValue, and a separate script elsewhere adjusts the velocity of the train’s movement. (speed.Value is the current speed it’s moving, set.Value is the target speed, canLoop is a debounce protection and also an easy way to kill the loop if necessary)
A problem I have had is that, when some other things are happening in the game, this loop slows down and causes the trains to decelerate way too slowly. Having never touched parallel lua before, I started testing, because it seems like a good solution, even if it isn’t perfect.
Well, what I did was change the train’s main parent model from a Model to an Actor, and whenever the loop is about to run, the script calls task.desynchronize().
However, upon testing, I noticed zero difference in game performance, or more importantly, performance of the trains. With only 3 or 4 of these trains in the game at the time, and the rest of the game not using any parallel whatsoever, you’d think these trains would get their own cores and be able to run almost flawlessly.
Did I do something wrong, does this not work for my use case, or is the problem that my scripts are not the direct child of the Actor?