Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
Accepted your answer because Mrmonkeys is weird LOL User#24403 69 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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.

0
Hope this helps :) Mrmonkeyman120 65 — 5y
0
Alright, thanks man Derpedee 6 — 5y
0
This is a very bad answer. Read the answer below. DeceptiveCaster 3761 — 5y
0
I did not say this before. I'm going to check., I believe someone is on my account. Mrmonkeyman120 65 — 5y

Answer this question