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!
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!
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.
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:
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)
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.
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
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
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
Thanks a lot! It worked precisely as I needed it
The other part would be a meshpart, I was just testing with more different sorts at once, nothing to worry about