"WaitForChild" & "FindFirstChild" , when to use?

Throughout coding, we often use a variety of functions to get an object. Some of these common functions are:

  • FindFirstChild
  • WaitForChild

On the contrary, we may also refer to an object without the use of any of these functions, for example.

local frame = script.Parent.Frame

instead of referring to…

local frame = script.Parent:WaitForChild("Frame")

or

local frame = script.Parent:FindFirstChild("Frame")

I understand that each of these functions have their own purposes. FindFirstChild simply finds the first object that matches a given parameter. WaitForChild waits until a specified criteria or parameter matches what it’s looking for. The relevant thread for WaitForChild is paused until that criteria is found. We also may not use any of these functions, and refer to our object without the use of it. What I want to know is what determines whether a specific function is required, e.g WaitForChild. Is using a certain function more efficient, if so what and how? Are any of these functions interchangeable, in what context can you substitute one for another?

I often use WaitForChild or simply none of the functions when getting an object. Is that a bad practice?

Thank you, Frost.

19 Likes

its not a bad practice, you use FindFirstChild when you don’t know if there is an object named something, like in touched event for a killpart, you use FindFirstChild, if you used WaitForChild, the script will stop trying to find the humanoid, every time something is touched, while just doing hit.Parent.Humanoid will error, WaitForChild will be used when you know something is gonna be there like when you use player.Characer:WaitForChild(“Humanoid”) because you know there is a humanoid unlike touched event, where you don’t know if the part has a humanoid

Edit: if something doesn’t exist in FindFirstChild, it will return nil, when you use WaitForChild, it will stop the script completely

2 Likes

FindFirstChild() is usually and best used to check if something exists.

WaitForChild() is used to yield until something exists. Only use WaitForChild() if you know something exists or is going to exist, or if you’re using StreamingEnabled. Add timeout parameters too and then check for existence after the yield just in case you might yield the whole thread.

1 Like

That is not true, using WaitForChild doesn’t stop the script but instead keeps searching through that object for another object named whatever you put in that WaitForChild function.

What do you mean if hit.Parent.Humanoid will error. I understand that, but I’m talking about using these functions in a variety of coding. Not limited to checking if a certain criteria is met.

by that, I mean it does keep searching, but it yields the script until it finds it unless you put a timeout parameter

1 Like

If the hit.Parent doesn’t find a humanoid at that time, it returns nil therefore printing an error.

Yeah but he’s still right? The entire script yields during the time WaitForChild is searching for the object. If the object doesn’t exist and is never going to exist, it’s going to search infinitely and yield the entire script forever unless you add in a timeout or unless it’s running in a separate thread

Yes, FindFirstChild should only ever be used to see if something exists. But in a variety of coding, for example a local script which holds the necessary information for a tween, should you use any of these functions when defining your variables? is one more efficient then the other?

It doesn’t really matter which one you use if you’re just doing something simple like defining a variable. Only thing I know to be careful of is connecting FindFirstChild to tight loops like RunService loops, because it is 20% slower than just indexing by dot. You shouldn’t be using FindFirstChild to define a constant variable, though. You only really should be using WaitForChild and the dot operator. Also, you really only need to use WaitForChild on parents. If you use WaitForChild on a frame for example, you don’t need to call it again on all of the children because they will almost always be loaded with the parent.

if you know something is gonna exist, you can just use WaitForChild or just nothing, like when you want the humanoid in a localscript, you can just do

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid") -- Sometimes the humanoid takes a while after a character, so we wait

FindFirstChild is when you want to know if something exists, or to “Find” something, in which it will return nil if it doesn’t find the object, unlike WaitForChild which yields the script until it finds it or has a timeout

So in this case it would be fine to use FindFirstChild or index by dot on a constant. So since WaitForChild waits until a criteria or object is met, if an exploiter got rid of that object, it would technically yield indefinitely.

i didn’t know about the loops thing, i guess you learn something new everyday

1 Like

I would assume that these functions are interchangeable, so would it bad practice to use FindFirstChild over indexing by dot?

As long as the parent of the object is loaded (that should be at the top of the script where everything is defined), you shouldn’t run into any problems indexing by dot, FindFirstChild, or brackets. But again, you should be using dot or brackets unless you’re trying to verify something’s existence, then you would use FindFirstChild.

1 Like

Imagine theres a part you created with Instance.new() in a script, in another script or a localscript, you want that part, sometimes the localscript will load before the part is created, if you use a dot to get the part, it will error, while using FindFirstChild and checking if it exists would be better

this is the worst example ever lol

if you know an Instance exists, you can use a dot or WaitForChild(), otherwise, you should use FindFirstChild

1 Like

Ahh, I understand WaitForChild would supposedly be the ideal function because it would wait for the parent to load, meaning that the children would have as well.

1 Like

WaitForChild
FindFirstChild

1 Like

Thanks, I understand how each function works. But, I was more asking if they’re interchangeable.

Not sure what your point is.

WaitforChild is mainly used on the client, FindFirstChild is used if you don’t know (ex): if something exists.