How can I get all children in a folder?

Hello. I am making a script that when a button is pressed, all the buttons in a certain folder are invisible. How can I get all the buttons instead of listing each and individually turning them off?

1 Like

Use :GetChildren() on the folder containing the buttons.

2 Likes

so like this?
image

1 Like

You need to loop through the array returned by :GetChildren() like so:

for i, v in folder:GetChildren() do
-- v is a button object you can do whatever you want with it
end

sorry I am confused. Like this?
image
( i do not know how to make a lua code block btw, sorrry in advanced)

No, v is a variable you can use it on its own and it will be a button object.

1 Like
for _, Buttons in pairs (YourFolderName:GetChilden()) do
Buttons.Visible = false
end

But if that folder contain other things than just buttons then you have to make a statement.

1 Like

No it would be like this

script.Parent.MouseButton1Click:Connect(function()
    wait(.5)
    local folder = script.Parent.Parent.Hair.A
    for i, v in pairs(folder:GetChildren()) do
        v.Visible = false
    end
end)
2 Likes

You could define a local variable at the top and store all the buttons

local Buttons = YourFolderName:GetChildren()

Then when a specific button is activated you could loop through the table and turn all the other buttons invisible:

for _, button in pairs(Buttons) do
button.Visible = false
end
2 Likes

pairs is no longer needed for iteration btw

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.