You can usually replace repetitive code with a simple loop.
We will use the Instance:GetChildren() method to get the children inside of step2.
local step2Children = step2:GetChildren()
Then we can create a for loop to go over each child in the resulting table
local step2Children = step2:GetChildren()
for i,v in pairs(step2Children) do
end
In the for loop above, i is the index and v is the value. The index isn't important to us right now, so we will focus on v, which will represent each child of step2. We just need to set Visible to false for v.
local step2Children = step2:GetChildren()
for i,v in pairs(step2Children) do
v.Visible = false
end
Now there could be a problem. If you have something in there that isn't a GuiObject, it will cause an error. This isn't a huge deal but it's better to handle it than to leave it. We just have to check if v is a GuiObject before setting Visible to false.
local step2Children = step2:GetChildren()
for i,v in pairs(step2Children) do
if v:IsA("GuiObject") then
v.Visible = false
end
end
And if you want to make it a bit neater, you can remove the first line and merge it with the second.
for i,v in pairs(step2:GetChildren()) do
if v:IsA("GuiObject") then
v.Visible = false
end
end
Hopefully this helps!
2 comments
I did that try that, but I have already used a loop on another reason so it gets confused with the first loop.
Im not sure what you mean by 'it gets confused with the first loop', can you clarify?