Other options besides :WaitForChild()

I’ve noticed that I rely on :WaitForChild() for almost everything. If I don’t use it, sometimes the script can’t find the item (about 30% of the time) because it searches for the item before it exists.

Ideally, I want to be able to access items directly, like this:
game.ReplicatedStorage.VFX.Something

instead of using:
game:GetService(“ReplicatedStorage”):WaitForChild(“VFX”):WaitForChild(“Something”)

1 Like

Truth is, a lot of the time you don’t need to use WaitForChild. This only mostly happens if you’re running local scripts from ReplicatedFirst (although of course still happens in starter scripts if I’m not mistaken).

Some other options are things like:

  • Waiting a fixed, certain amount of time before doing things (unreliable)
  • Manually “loading” assets into the game with ContentProvider
  • Alternatively, waiting for elements to load on a separate script before enabling the one that’s supposed to run what you want.

This video gives you a nice breakdown on how, when and why you should use WaitForChild: https://youtu.be/7tzy1DuPcBQ

Hope this has helped you!

2 Likes

You need it for PlayerScripts sometimes and an alternative is using the FindFirstChild method. I wouldn’t enable scripts because it is confusing if anyone else was to look at your project. I would have different parts that load asynchronously and then when they are all loaded it synchronizes again. You only need to use the WaitForChild method once on an instance but I don’t think it has any performance hit.

2 Likes

I would look into what this person sent. I would look into the 4:36 mark in particular. game:IsLoaded() is nice to know.

1 Like

This only happens for dynamically created Instances. Outside of ReplicatedFirst, everything that is statically published will replicate to the client before scripts in the client context runs.

The main issue I see is people having their scripts on StarterPlayerScripts trying to referencing something like their PlayerGui. In most cases, the scripts in StarterPlayerScripts will clone first to their PlayerScripts before the contents in the StarterGui clone into their PlayerGui (because the Character has to spawn first).

1 Like

Thanks a lot for clearing this up!