What is the order of execution of the scripts when a game starts?

I have 3 scripts in my game:

  1. ServerScriptService → Script
  2. StarterGui → LocalScript
  3. ReplicatedStorage → ModuleScript

In the Server script, I have instructions in the body of the script and Players.PlayerAdded:Connect(function(Player) to be executed when the player is added.
In the LocalScript, I have the same: instructions in the body of the script and RemoteEventPlayerAdded.OnClientEvent:Connect(PlayerAdded).

What’s the sequence of execution in this case?


Also, in the Server script, I’m creating some parts in the Workspace.
But it seems that LocalScript is being executed in parallel while Server script is still being executed.
So, in LocalScript, I have to use WaitForChild for EVERY element in the workspace.
However, I have almost 100 parts to write WaitForChild one by one, once WaitForChild only waits for the current element, not for its children tree…
This is boring and laborious.
Is there any way to ensure that LocalScript will not be executed before (or in parallel with) ServerScript?

2 Likes

You can use a RemoteEvent. On the server script, use FireClient() on a Remote event AFTER all of your parts have been made. Then simply connect the event on the client with a function connected to .OnClientEvent(), and execute your LocalScript. If I wasn’t clear tell me so I can show you a small example of what I mean.

Yeah, there is no guarantee which scripts run first unless you put them in replicated first.

Even then the order which they run is replicated first is … I haven’t researched that far yet.


However, there are solutions to this via frameworks which yield scripts via functions like preload and start to guarantee sequence of execution I suggest looking at them:

As for the problem you have with waiting for 100 parts to load you might want to check out this other thread which details the same problem except for vehicles, to which they used an odd replication priority check with an Int value and a check for the number of Descendents of the model.

Link to the thread

Seems like an odd fix but I wonder if there are better methods.

5 Likes