I know this is a simple question but i'm just so confused aren't they just the same thing? lol
Indexing a child and using FindFirstChild()
both do the same thing: They find the first child by the given name. However, if the child has whitespaces in its name, FindFirstChild()
would be used because it takes a string as the argument to its primary parameter and you can't index a child that has a name containing whitespaces using the period (.
). FindFirstChild()
should be used in that scenario.
Secondly, FindFirstChild()
can also search through the descendants of the Instance or service it is being called from using its second parameter, which takes a boolean as an argument. This second parameter is its boolean recursive. It is set to false
by default, which means that it will only search and return the first child (the first Instance (or service if using game:FindFirstChild()) by the given name in the first sub-directory of the hierarchy), not the first descendant (the first Instance/service by the given name anywhere in the hierarchy). Setting this parameter to true
enables FindFirstChild()
to search through the descendants in a given hierarchy. Indexing a descendant (or ancestor) usually requires multiple periods, such as:
script.Parent.Parent.Model.Primary.Coins
FindFirstChild()
is good practice overall. If the given name exists, it will return the Instance or service by that name. Otherwise, it returns nil. This is also a counter to the common attempt to index a nil value
error, as you can check if that object exists.
Typically we just want to do
game.Workspace:FindFirstChild('Part')
Whenever we need to find something inside of a LocalScript, when you are using a ServerScript you can just use:
game.Workspace.Part
That is how I do it.