It really depends on what you need it for (which isnt a lot), but a good rule of thumb is to use it when you can ensure the object exist/will exist within a certain time frame, as it has the ability to halt code when the object is not found because of a timeout.
:WaitForChild()
in standard practice, should only ever be used on the Client when you can ensure the object exists. Unlike the Server which has everything loaded in, the Client needs to load in everything that is able to be Replicated for it, if your code fires very early, you will likely run into errors indicating that the object doesnt exists yet, which is where :WaitForChild()
comes in, which waits until it finds the object, or until it reaches the timout.
Which in essense
-
Object Loads in
-
:WaitForChild()
finds this item
-
Code functions as intended
Bad usages of :WaitForChild()
that I can think of are:
-
Server Usage
The Server will always have everything loaded in, Waiting for something that is already existing is pointless, and just uses more resources, aka bloat.
-
Sanity Checks
If you are just checking if an Object exists, :WaitForChild()
is an awful way of doing it as explained earlier, In order to perform a proper Sanity check with Instances
, you can use :FindFirstChild()
, difference being that it does not yield, and does not return an error if the object doesnt exist, with it instead returning nil
, Because it returns nil
, we can check for the Condition if Object ~= nil then
, to perform this sanity check.
Most likely, but for a good reason.
ReplicatedFirst
, is the first thing the Client will Fire Code from (Its in the name lol), before the Game has loaded in, which is how we are able to create stuff like loading screens with seemless transitions, and preload our games.
Because it fires so early, very little would have been replicated, besides the Services provided to you, so in order to ensure that you are grabbing an object outside of ReplicatedFirst
, you need to wait for it to exist, and as expected, :WaitForChild()
does this job perfectly, however if you wait long enough using some code that maybe yielded for a while, you might not have to use it, as by that point it should have already loaded in.
Most likely not , but if any errors occur over it not existing (Its pretty rare for me, but i cant say the same for everyone else), then I would say use it, but this is something I dont see happening to often, so its a precaution worth taking.