Local script under SсreenGui, runs only after all Gui descendants are fully replicated to PlayerGui?

When writing a local script in “ScreenGui” which is in “StarterGui”, I want to make sure that all the right parts have replicated to the client and I can reference them.

game:IsLoaded()
Not suitable because the game is considered loaded even if not all “PlayerGui” objects have been replicated to the client

example

Here you can see that the loading of the other Gui has not yet finished, but the game is considered loaded

And writing :WaitForChild(“”) for ALL objects is obviously not the best solution

I started looking into how objects are replicated to the client, and I noticed a pattern:
Any local script under a shared “ScreenGui” will only run after that “ScreenGui” is fully replicated to the client.

The code on scripts “0”, “1”, “2” is the same (green line).
The output, which object is replicated, is done by “LocalScript” (orange line).
With the red lines, I showed that the “0” script was loaded at the beginning. And at the end the “2” and “1” scripts were replicated.
And only after the last object (in this case “1”) was replicated in ScreenGui called “adin”, all 3 local scripts started. First “0”, then “2”, then “1”.
And after that, the replication of the next “ScreenGui” started

I don’t understand if it was intended that way, or if it happened by accident
And it comes out: :WaitForChild("") is not needed? If you only refer to objects under the same ScreenGui.

Bottom line: I don’t have to worry about any “ScreenGui” descendant not being replicated, since the local script won’t run until all “ScreenGui” descendants are fully replicated?

If this is the case, it is strange that the documentation does not mention it, because it is very important

2 Likes

Maybe “ScreenGui” is replicated to “StarterGui” and then locally just copied to “PlayerGui”.
But when I moved the Gui from “ServerStorage” to “PlayerGui”, the local script also started only after all descendants were loaded.
I’m going to assume that this was intended, and local scripts won’t run until all descendants of the shared object are loaded/replicated (like “ScrenGui” or “Folder” in “Workspace”).
I’m not claiming anything!, but I’m relying on the outcome of the tests.

3 Likes

Unfortunately, it is.

In LocalScripts, you NEVER EVER EVER reference children using periods to avoid issues like these.
You see, LocalScripts (and Scripts) start executing before anything is loaded in. In Scripts this is not a problem because server handles it. In LocalScripts it does not.

1 Like

I did a bunch of different tests and the results came out weird.
There is a main object “ScreenGui” in “ServerStorage”, and its descendants (two of which are local scripts).
In “ReplicatedFirst,” two “LocalScript”.
First: Output which descendant was added to “LocalPlayer”.
Second: waiting for the main object to appear using :WaitForChild in a particular location (PlayerGui\Backpack\ LocalPlayer\Workspace) is prescribed.
In “ServerScriptService”, a script that copies from “ServerStorage” the main object to the mentioned locations.

Results:
:WaitForChild - Triggered without waiting for replication of all descendants of the main object.(except for PlayerGui location).
Local scripts were triggered MOMENTALLY after replication to Backpack (script replicated → ran → next descendant replicated).

But, :WaitForChild and local scripts exactly in “PlayerGui” were launched ONLY after full replication of all descendants of the main object.
And I don’t understand why exactly “PlayerGui” has this behavior, but elsewhere, local scripts ran without waiting for anything, as did :WaitForChild.

Even after finding an interesting topic that clarified a lot, this point remained unclear.

It’s just a weird moment, when replicating specifically from the server specifically to “PlayerGui”.
In general, if the object is in “StarterGui”, then yes, the local script as its descendant will not run, because if you open the documentation.

screenshot

Everything becomes clear, because copying and replication are completely different things.
EDIT: The above doesn’t make sense because, the same thing is written in the documentation about “Backpack”.

screenshot

But it works differently, here’s a test:

1 – Auto-copied provided by Roblox (from “StarterGear” to “Backpack”).
2 – Server-to-client replication in “Backpack”.
3 – Manually cloned by local script into “Backpack”
in 1 and 2 cases, the local script runs immediately after adding it.
in 3 the local script is launched only after all descendants of the cloned object have been loaded.

That is, “PlayerGui” is really something special, because if you do the same with it, then any local script will run only after all the descendants of the main object appear.

2 Likes