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

Differentiating findFirstChild and square brackets?

Asked by
Dummiez 360 Moderation Voter
10 years ago

I don't completely understand the actual differences when it comes to using findFirstChild and the square brackets, per say:

local part = Instance.new("Part").Parent = game.Workspace

local a = game.Workspace:findFirstChild("Part")
local b = game.Workspace["Part"] -- Using an if statement to check if this exists doesn't work.
local c = game.Workspace.Part

When should I use which?

0
I like this concept, but I don't know... Rep for the question though. OniiCh_n 410 — 10y

3 answers

Log in to vote
3
Answered by 10 years ago

-Use the "." when using a single-word part.

-Use when using a multiple-word part.

-Use FindFirstChild when you aren't sure of the existence of the part or if you want to find a descendant, not just a child.

Example of FindFirstChild for finding descendants:

local model = Instance.new("Model", workspace)
Instance.new("Part", model)
print(Workspace:FindFirstChild("Part", true).Parent.Name) --Prints Model, because of "true" argument.

Example of FindFirstChild if statement:

if Workspace:FindFirstChild("Articulating") and Workspace:FindFirstChild("Torso", true) and Workspace:FindFirstChild("Torso", true).Parent == Workspace:FindFirstChild("Articulating") then
    --code
end
0
I appreciate your explanation of the use of FindFirstChild, thank you! Dummiez 360 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

The FindFirstChild method returns false if it's not able to find the child, while the brackets will throw an error if it's not able to find the child.

Log in to vote
-1
Answered by 10 years ago
local = game.Workspace:FindFIrstChild("Part")

For the top code you are still finding Part.

local b = game.Workspace["Part"]

For the top code you are still finding Part.

Answer this question