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

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.

Please read the post thoroughly first. I understand that each function has it own purpose. I’m more asking on whether it’s okay to use one over another, (inclusive of the context). I’m not saying use FindFirstChild in placement of WaitForChild, these have two completely different functions. I was also asking if one is more efficient then the other. I understand now though, and know better to use which function.

Yes, it’s “okay to use” X over Y, to accomplish Z.
However use them wisely.

Use this if it already exists.

Use this when you are waiting for something on the client side, or waiting for something to exist. Keep in mind the specified object has to exist at some point.

Use this when you are unsure if an object is existent or not, for example, checking the part has a humanoid inside of it.


There’s a really nice article that explains all of this:

10 Likes