Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What are the practical uses of WaitForChild and FindFirstChild?

Hello! Newbie scripter here who is learning the science of Lua. I am watching a small beginner's youtube tutorial and we just came to these two commands for this part of the course. Those commands being WaitForChild and FindFirstChild. I almost completely understand how these commands work but I don't see how they would be useful. If anyone reading this is capable of explaining how these commands would be useful if I were programming a game that would be great!

1 answer

✓ Accepted answer

FindFirstChild is used to check for the existence of an object before you do something with it. Cases where you are positive the object will exist, you can can refer to it directly, like workspace.Part. Otherwise, you can do if workspace:FindFirstChild("Part") then or make a variable and check for its existence inside of an if statement. Do not use FindFirstChild if you are not checking if the object exists!

WaitForChild is one you would use if you need the object as soon as the script fires, typically at the top of the script. WaitForChild will yield until the given object exists unless it times out. This is really only used on client side scripts, as you can directly refer to objects on server scripts at the start of the script. Again, refer to the object directly or use FindFirstChild and check existence with an if statement if you are not defining it at the beginning of the script.

Lastly, I want to talk about FindFirstChildOfClass. FindFirstChild will look for an object with the exact name you give as an argument, but FindFirstChildOfClass will look for an object with the given class regardless of its name. An example of this is FindFirstChildOfClass("ForceField") which will find the ForceField object regardless of its name.

Hope this helps, and if I got anything wrong please comment.

3 comments

0

In the case of using WaitForChild in early allocation, it's only considered practical when done on the Client, as the Server will not encounter Instance loading issues; you should commonly use direct referencing within ServerScripts, unless you're certain the Instance may take time to appear.

0

Ok, I figured this was the case because it's typically what I do, but I wasn't sure it was good practice if the object may not exist. I'll edit my answer.

0

Thanks my man.

This discussion is preserved as it was. New answers, comments, and votes are closed.