Hi Guys, so maybe you understood by the title, it’s very basic, but in curiousity, I want to ask the difference. For example - character:FindFirstChild(“Humanoid”) and character.Humanoid
What does this mean ? Aren’t these both same ? Is there any difference ?
FindFirstChild finds the first child in the hierarchy to have that name, the other takes all of them basically
FindFirstChild
Looks for the first Instance
under a specific pattern, if the pattern is not found, it will return as nil
, otherwise it will return an Instance
So for Example, you can do:
local Ex = Example:FindFirstChild("Example") -- Instance or nil
if Ex then -- if Object Exists
print"object Exists"
else -- If not
print"object doesnt exist"
end
Normally when you use Instance.Child
on something that doesn’t exist, it will error, however if you use FindFirstChild
, you can prevent this error:
-- Lets Say the Instance doesn't Exist
print(workspace.Example) -- Errors as Instance doesn't exist
print(workspace:FindFirstChild("Example")) -- nil (Error prevented)
You can even use it to search recursively for Instances
.
Simply: Can also be used to look through Descendants:
local Ex = Example:FindFirstChild("Example", true)
Documentation if that helps:
FindFirstChild()
is useful for indexing an instance that may or may not exist, where as normal indexing will throw an error if what you are looking for is not there
here is my workspace. ‘jeff’ is not there
so when i try to index workspace.jeff
i get an error
instead i can use FindFirstChild
to avoid errors and i will recieve nil telling me there is nothing called jeff
What I said:
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.