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

What's different about game.Workspace.Part from game.Workspace:FindFirstChild('Part')?

I know this is a simple question but i'm just so confused aren't they just the same thing? lol

2 answers

✓ Accepted answer

There are a couple of differences.

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.

1 comment

0

Accepted your answer because Mrmonkeys is weird LOL

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.

4 comments

0

Hope this helps :)

0

Alright, thanks man

0

This is a very bad answer. Read the answer below.

0

I did not say this before. I'm going to check., I believe someone is on my account.

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