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

Is it possible to find children?

Asked by 7 years ago
Edited 7 years ago

What im talking about is is it possible for a script to detect all children from a model. For example: Lets say the code is FindChildren

local childs = script.Parent.FindChildren

childs.Transparency = 1
childs.canCollide = true

So now, all the children from the Model now have a transparency of 1 and Collision. Is there another feature for that?

0
Do you mean the childrens children as well e.g. a part inside a part? User#5423 17 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

There is a function for this already.

local children = script.Parent:GetChildren(); -- This is gathering all of the children in script.Parent into an array (i.e., children = {script, textLabel, etc.})

for i,v in pairs(children) do -- The for loop is used to sort through the array in order and we can manipulate parts as need be. The variable i shows the number in the array that the entry is, whereas the v variable is for the part itself. Given how v is an object we can't just do print(v) we would need to do print(v.Name) to see what v's real name is.
    v.Transparency = 1; -- Setting the transparency of object v to 1
    v.CanCollide = true; -- Setting it so that v CanCollide with other blocks
end;

This will make every part in the scripts parent so they can collide and they are invisible. If you are looking for a specific part you can use this function

local child = script.Parent:FindFirstChild("Sphere"); -- This finds the first child in script.Parent with the name of "Sphere". Now you can do whatever you want to it.

If this helps, please accept this answer.

0
Hmm, I see no explanation for what the for in pairs loop does, and how you get an object from an array. There is no problem with this answer but it would be better with a detailed explanation. Thank you. EzraNehemiah_TF2 3552 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Yes. You tried, and that's good. However, the correct function to use is :GetChildren(). This will return an array - or table - of children.

To find a specific child, you can use :FindFirstChild(name). If the child is not found, it will return nil.

Answer this question