In this tutorial, I’ll explain how and when to use these functions:
- . (dot)
- [] (brackets)
- WaitForChild()
- FindFirstChild()
- FindFirstAncestor()
- FindFirstChildOfClass()
- FindFirstAncestorOfClass()
- FindFirstChildWhichIsA()
- FindFirstAncestorWhichIsA()
To understand those, you first need to understand the hierarchy of roblox.
I hope these two pictures will do it, if not, you can search for better explantations.
This would look like this in the roblox explorer:
Dots:
You use dots to either access the Parent or the child of an object.
local Part = script.Part --Child
local Part = script.Parent --Parent
You always use dots to access children, expect:
- You aren’t sure if the children exists
- It has a special name
- It has the same name as a property
Brackets:
If the children has a special name, some people will use FindFirstChild() instead, which is a bad idea since FindFirstChild() takes longer. Instead, you should use Brackets.
local Part = script["1Part"]
It’s also possible to use, when you need to search for a variable name:
for i = 1, 10 do
workspace["Part"..i].BrickColor = BrickColor.new("Really red") --This will make the parts Part1, Part2, etc. until 10 the color red
end
You use brackets when:
- The name start with a special character (numbers and other special characters)
- The name has a special character in it (this excludes numbers)
- You need to search the object with a variable
Special characters examples: Space, #, +, &, |, ¢, etc.
FindFirstChild():
FindFirstChild() is used when you are not sure if a child exists. It will either return the child or nil. When you use dots, it will error.
workspace.Baseplate.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then --We don't know if a player touched it, therefore we first need to check if it has a humanoid
--Do something
end
end)
When you checked if an object exists, there are 2 ways you can continue:
if hit.Parent:FindFirstChild("Humanoid") then --
hit.Parent.Humanoid.Health = 0 --Getting the humanoid with dot
end
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if Humanoid then --
Humanoid.Health = 0 --Making a Humanoid variable
end
The FindFirstChild() function has a second parameter. If you set it to true, it will be recursive, means if there’s no child with that name, then it will also search the descendants. It’s similar to the disabled :FindFirstDescendant.
If your explorer looks like this:
print(script.Parent:FindFirstChild("Part")) --This will print nil
print(script.Parent:FindFirstChild("Part", true)) --This will print the Part
If you name your child after a property, dots and brackets will get the property, but FindFirstChild() will find the actual object.
Example:
Transpareny is the child, but at the same time a property.
local Part = workspace.Part
print(Part.Transparency) --Output: 0
print(Part["Transparency"]) --Output: 0
print(Part:FindFirstChild("Transparency")) --Output: Transparency (Object)
FindFirstChild() takes longer about 20% longer than dot, so I advise you to not name objects after properties.
FindFirstAncestor():
This works similar to FindFirstChild(), but you usually don’t use it to check if something exists. This function will return the first ancestor which is named like the first argument.
Explorer:
print(script.Parent:FindFirstAncestor("Model")) --This will print the model
FindFirstChildOfClass() and FindFirstAncestorOfClass:
Since we know what the difference of FindFirstChild() and FindFirstAncestor() is, we just need to know what Ofclass() does.
In Roblox, there are base classes and singular classes. There’s the base class “BasePart” for example. This includes Parts, Unions, MeshParts, etc. OfClass() only works for singular classes.
print(script:FindFirstAncestorOfClass("Part")) --This will print the part
print(script:FindFirstAncestorOfClass("BasePart")) --This will print nil
FindFirstChildWhichIsA() and FindFirstAncestorWhichIsA():
The Difference between OfClass() and WhichIsA() is, that OfClass() only works for singular classes while WhichIsA() works for both.
print(script:FindFirstChildWhichIsA("Part")) --This will print the part
print(script:FindFirstChildWhichIsA("BasePart")) --This will also print the part
WaitForChild():
This function checks if a child exists, if it doesn’t, it yields the script until it exists. If there’s no timeOut paramter, it will stop after 5 seconds, return nil and give a warning like this:
Infinite yield possible on ‘Workspace.Script:WaitForChild(“Part”)’
Usually you use WaitForChild() on the client if you aren’t sure if a child already exists.
local Part = workspace:WaitForChild("Part")
print("Part exists!")
If you set StreamingEnabled to true, it might error because the part only exists on the server.
There’s a second parameter that determines how long it should search for the child. This is set to 5 by default.
local Part = workspace:WaitForChild("Part", 10) --This will search for the part for 10 seconds. If it doesn't exist it will return nil after 10 seconds
Conclusion:
You now know how to use the functions FindFirstChild(), WaitForChild() and a few other. If you use them right, your scripts will look cleaner and the performance will also improve.
This is my first scripting tutorial, please tell me if there’s a gramatical or code error. I hope this was understandable.
Was this tutorial understandable/helpful?
- Yes
- No
- Mid
0 voters