I have a GUI with multiple frames in it, and want to make all of them close when I press the menu button.
Here is how my Gui is set up
Gui Menu Button Script Frame 1 Script Button 1 Button 2 Script Frame 2 Buttons within Frame 2
The script in the Menu Button
local MenuSelect = script.parent.parent.MenuSelect local Menubtn = script.parent script.Parent.MouseButton1Click:Connect(function() local Buttons = script.Parent.Parent.Buttons if Buttons.Visible == false then Buttons.Visible = true else Buttons.Visible = false end end)
Basically what this does is open Frame 1
Script in button 2
local Emotes = script.Parent script.Parent.MouseButton1Click:Connect(function() local EmotesGUI = script.Parent.Parent.Parent.EmotesGUI if EmotesGUI.Visible == false then EmotesGUI.Visible = true else EmotesGUI.Visible = false end end)
This opens frame 2 to the side
I want the Menu button to close ALL frames,
I've tried using something like this
if Button2.Visible == false then Frame2.Visible = false
which basically means since the menu button closes button 2, it should close frame 2 aswell, but I cant get it to work.
Any ideas?
You can do this quite easily by setting the ScreenGUI's Enabled
property to false, which makes every GUI Instance within invisible.
If you don't want to use that, use this:
local names = { Names_of = true, the_GUIs = true, you_want = true, to_close = true, }; function closeAll() for i,v in pairs(script.Parent.Parent.Parent.EmotesGUI:GetDescendants()) do if(names[v.Name] and v:IsA("Frame")) then v.Visible = false; end end end
Just call closeAll()
everytime you want to close all the GUIs in EmotesGUI.
This code will loop through all children of your Gui one by one and check if it is a Frame
. If so, it will change the Visible
property to the opposite of what it is. So if it is true, it will become false. Whithout having to use any if
statements.
So this should be the code in the LocalScript, in the button that you want to close all the Frames in your Gui. Though I have designed it to fit your Menu Button Script.
local guiChildren = script.Parent.Parent:GetChildren() script.Parent.MouseButton1Click:Connect(function() for i, v in pairs(guiChildren) do if v:IsA("Frame") then v.Visible = not v.Visible end end end)
You can use an iterator like pairs
to loop through every child in the GUI. You can then check type to see if it's a frame and if it is, set it's visible property to false. It should look something like this.
for _,Object in pairs(path_to_gui:GetChildren()) do if Object.ClassName == "Frame" then Object.Visible = false end end
note:
you can make _ and Object anything you want. It's user defined but it helps to make them something meaningful.