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

Is it possible to find children?

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?

1 comment

0

Do you mean the childrens children as well e.g. a part inside a part?

2 answers

✓ Accepted answer

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.

1 comment

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.

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.

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