Ever since I have acknowledged the existence of :WaitForChild(), I’ve been using it for every single script I’ve made. I was wondering, is it efficient, and is there a point in using it? I’ll give a couple of examples:
Let’s say I have a module in ReplicatedStorage > Modules (folder). To access that module, I would have a pretty lengthy line of code…
local MyModule = require(game:GetService('ReplicatedStorage'):WaitForChild('Modules'):WaitForChild('Module')
Another example, let’s say I have some sort of sprinting script in StarterCharacterScripts. It needs to access the humanoid to change the speed…
local Humanoid = script.Parent:WaitForChild('Humanoid')
Humanoid.WalkSpeed = 20
…is it necessary to use it in these cases, and is it efficient?
Also, let me know if this is the wrong category.
You use :WaitForChild() if the thing you are trying to reference doesn’t exist yet.
Example:
You have a script that gets loaded before a certain part does.
If you just reference it normally: part = workspace.Part it will error, since the part doesn’t exist yet, and for that you would use: part = workspace:WaitForChild(“Part”)
We use it whenever we dont expect the object to exist yet, Like a folder or module in replicatedStorage. we dont know or can gurantee that it will 100% load before script injecting. So we use it just incase if it didnt.
you use WaitForChild when the Instance you need a reference to has to either replicate from another computer (server to client, or client to server), or it’s being created at runtime by another script on the same computer (e.g. a server Script is creating new Instances in Workspace, and other server Scripts are waiting for them to be generated).
The biggest gotcha with WaitForChild is that if you don’t supply a duration, and you wait for something that isn’t coming (due to error, wrong name, etc) it can block forever and prevent other parts of your script from executing. For this reason, I often do client-side WaitForChild things in a coroutine, and I put generous (but not infinite) time limits on things I expect to replicate quickly, like 60 seconds, so that the failures are not silent. When you do this, uses of the reference following the WaitForChild call have to account for the reference possibly being nil.
:WaitForChild() in general is often used wait for an instance to be created. For example you might want to wait for something to load in before running code.
It’s not really needed in this case. If you have the script inside of StarterCharacterScripts, they should only be added when the character is loaded so I think it should be fine. In regards to if it’s efficient, using :WaitForChild() does not really effect the efficiency of your code (the only way it could if your using lots of :WaitForChild() and it will never find the child).
It completely depends on where your script is located. Some containers like StarterPlayerScripts provide guarantees about replication (they only run the scripts after game.Loaded fires).
Assuming the first example you posted runs after being placed in StarterPlayerScripts, the Modules folder and Module will already exist so WaitForChild is not needed at all.
For the second example, I personally don’t use it at all but the default Animate script seems to use it to get the Humanoid.
You can use :waitforchild() to yield a script until the targetted object with the used name has been found so you dont has to use " repeat wait until object ". To do this you just need to write this script:
local object = “Namehere”
local YieldTime = math.huge – yields forever if not exists
local targetparent = game.Workspace
local finder = targetparent:waitforchild(object, YieldTime)
print("Found "…object)
I just have written this on my phone but its pretty basic so I hope I made no mistakes . Its very useful if you have Content streaming enabled ( a bool inside workspace ).
You use waitforchild for anything that isn’t pre-made within studio. Everything laid out inside studio (with the exception of items in StarterGui) have already fully loaded before the script runs. Anything not already there (such as player character, PlayerGui, script instantiated instances) must use waitforchild. So, in your case, you probably don’t need waitforchild (assuming it runs in a container other than replicated first.
If your script is in replicated first, instances haven’t loaded in yet, so you must use waitforchild. You can also use game.Loaded:Wait() in scripts in replicated first to wait for things to load.
@MrSlimer123 you do not need waitforchild for items in the workspace, with the exception of streaming enabled. @commitblue you do not need waitforchild for items pre-made in studio within ReplicatedStorage.