Change property of all children at once?

Hi all,

I was wondering; How does GetChildren() work? Say I have a model with 5 children, and I want to change the transparency of all children to 1, how would I do this?

Thanks for reading!

7 Likes

You will want to iterate over the table returned.

for _, child in ipairs(model:GetChildren()) do

end

Now I will assume not everything in there is a base part so I would check to make sure there is.

if child:IsA("BasePart") then

end

So great. We can guarantee the child is a base part, now set their transparency to 1.

child.Transparency = 1

Now you will have to put the code together.

5 Likes

Would this work then?

while true do
	for _, child in ipairs(workspace.Model:GetChildren()) do
		if child:IsA("Part")then
			child.Transparency = 1
		else
			child.Color = Color3.new(255,0,0)
		end
		wait(5)
		if child:IsA("Part")then
			child.Transparency = 0
		else
			child.Color = Color3.new(0,255,0)
		end
		
	end
	
	
	wait()
end

(incase i want to change transparency of the ā€œPartā€'s and i want to change the color of others)

With the code I showed above, it always just does one, how would I make sure it does for all of the children?

having a wait in a for do loop will make it wait 5 seconds before running the next child through the loop, removing the wait 5 should have it work properly

If you mean every single child of that model, Use :GetDescendants() instead.

When I tried doing GetDescendants() it just did 1 brick, and didn’t even repeat the process like before

Are you sure you did

 ModelName:GetDescendants()

?
It works 100% of the time.

I have this in workspace: image
and this is my code:

while true do
	for _, child in ipairs(workspace.Model:GetDescendants()) do
		if child:IsA("Part")then
			child.Transparency = 1
		elseif 
			child.BrickColor = "Lime green"
		end
		wait(1)
		if child:IsA("Part")then
			child.Transparency = 0
		elseif 
			child.BrickColor = "Really red"
		end
		
	end
	
	
	wait()
end

(Dont mind the color, I tried with both RGB value and name)

1 Like

Try this;

while true do
	for _, child in ipairs(workspace.Model:GetDescendants()) do
		if child:IsA("BasePart")then
			child.Transparency = 1
		else
			child.Color = Color3.new(255,0,0)
		end
		wait(5)
		if child:IsA("BasePart")then
			child.Transparency = 0
		else
			child.Color = Color3.new(0,255,0)
		end
		
	end
	
	wait()
end

You have to do :IsA(ā€œBasePartā€) not part. Part checks if it has the classname part. Meshes have the clash name MeshPart.

1 Like

It still does the same, except the meshparts also turn invisible instead of getting colored

Then do two separate checks,

if object:IsA("MeshPart") then
-- Do whatever
end

if object:IsA("Part") then
-- Do whatever
end

You can also change it back to children.

I saw a friend using coroutine in unity, if i got it correctly, when i use coroutine i just place the same code inside the coroutine except the while true do?

	for _, child in ipairs(workspace.Model:GetChildren()) do
		spawn function()
			if child:IsA("Part")then
				child.Transparency = 1
			else
				child.BrickColor = "Lime green"
			end
			wait(1)
			if child:IsA("Part")then
				child.Transparency = 0
			else
				child.BrickColor = "Really red"
			end
			
		end
		
		wait()
	end
1 Like

if im not mistaken you cant do elseif without an argument?

Elseif without an argument is else

I edited the code

EDIT: My bad for overcomplicating the solution, see Lightlimn’s response below

2 Likes

I’m not sure what, but something went wrong here, it kind of randomly changes the transparency of some of the parts in my model, any idea why this happens?

I’m sorry but these replies are really not very helpful and are way overcomplicating a simple task.

Your original function was almost right. Here’s a slightly edited version, using 2 for loops instead of one.

while true do
	for _, child in ipairs(workspace.Model:GetChildren()) do
		if child:IsA("Part")then
			child.Transparency = 1
		else
			child.Color = Color3.fromRGB(255,0,0)
		end
	end
	wait(5)
	for _, child in ipairs(workspace.Model:GetChildren()) do
		if child:IsA("Part")then
			child.Transparency = 0
		else
			child.Color = Color3.fromRGB(0,255,0)
		end
	end
	wait(5)
end

That’s all it should take. You don’t need coroutines or spawn functions yet - that’s completely unnecessary for this.

I also changed Color3.new to Color3.fromRGB, because that way putting 255 as one of the values makes more sense. using .new takes values from 0 - 1.
However, I’m not sure what ā€˜child’ could be if it’s not a part and has a colour that needs changing. That could error if it comes across something like a script. Let me know how this works

14 Likes

Thanks a lot! It worked precisely as I needed it :slight_smile:
The other part would be a meshpart, I was just testing with more different sorts at once, nothing to worry about

1 Like