Making a button close the current frame that is opened

Hi,

What I want to do

I would like it so only one frame at a time can be seen. Imagine this. 3 buttons. When you click any button it opens a gui. I want it so when you click a button it opens a gui(already done), then when you click another button it closes the guis(First one clicked) frame.

I hope you can help me!

From,
Mista

3 Likes

Are they using the same frame? The simplest thing you can do is make one button make the frame visible’s property to true, and the other sets visible to false.

button1.MouseButton1Click:Connect(function()
	frame.Visible = true
end)

button2.MouseButton1Click:Connect(function()
	frame.Visible = false
end)

Hopefully this is what you wanted, you don’t really need checks to close the gui if another button does it

Hi,

I would like it so that when a button is pressed and another guis frame is opened it will close it out and show the buttons that was just clicked frame. Im not sure how to explain this.

From,
Mista

If I understand correctly, there are 3 buttons that open different frames and you want it so if there are other frames open, they should be closed?

umm…Heres a game that can help you understand what I mean…Click one of the buttons on the left and click another one and you will see the original guy close while the buttons that was just clicked, its frame will open.

Game ID: (795) Find The Doge Heads 2 - Roblox

Hopefully you can understand!

Maybe this will help, When this button is pressed, close all visible frames

1 Like

That’s how you would do it yea. The way you do it depends on how the Screengui is handled. But if the buttons are all children of the Screengui and the frame to open is a child of the button, something like this would work (The script in question is a localscript inside of the screengui

local gui = script.Parent

for i,v in pairs(gui:GetChildren()) do
	if v:IsA("TextButton") and v:FindFirstChildOfClass("Frame") then
		v.MouseButton1Click:Connect(function()
			for i,f in pairs(gui:GetDescendants()) do 
				if f:IsA("Frame") and f.Parent:IsA("TextButton") and f.Parent ~= v then
					f.Visible = false
				end
			end
			v.Frame.Visible = true
		end)
	end
end

Let me explain this code

First we get the screengui, then we loop through all its children, if a child is a textbutton and it has a frame inside it, then we connect the MouseButton1Click event to it. What this event does is when the player presses a button, it loops through everything in the screengui and if what it finds is a Frame, its parent is a textbutton and its parent is not the button that was pressed, it makes them invisible, afterwards it enables the frame inside it

1 Like

Hi,

Im not sure this would work. This… Is what it looks like
Screen Shot 2021-02-22 at 1.10.38 PM

I would like the text buttons to close the other guis frames…If their opened. You know what I mean…

I believe something like thsi would work for taht

local gui = script.Parent

for i,v in pairs(gui:GetChildren()) do
	local frame = v:FindFirstChildOfClass("Frame") or v:FindFirstChildOfClass("ImageLabel")
	if v:IsA("ScreenGui") and frame and v:FindFirstChildOfClass("TextButton") then
		v:FindFirstChildOfClass("TextButton").MouseButton1Click:Connect(function()
			for i,f in pairs(v:GetChildren()) do 
				if (f:IsA("Frame") or f:IsA("ImageLabel")) and f.Parent ~= v then
					f.Visible = false
				end
			end
			frame.Visible = true
		end)
	end
end

The Localcscript taht contains thsi code would have to have StarterGui as its parent

Oh…Ok. Thank you. Im going to try this out!

Is this how I should place it? Just in the starter gui?
Screen Shot 2021-02-22 at 2.36.13 PM

I believe so yea, try it out and see how it goes. I think I made the code a bit horribly so it may need a bit of improvements

Ok I just tried it. It stoped my buttons from functioning.

Then I think that’s my fault with the code, guessing how I tried to do it didn’t go so well, another way you could do that is just to connect the event to each one by one, I tried to shorten that but I think it’s better if you do it event by event. I’m not sure what would be the proper way though, I think proably one of those 2 people that are reply have a better answer than I do

Ok so I have read all of the comments, the person above me has a really good way to do this but it may be somehow complicated for some people.

So you will need to place a local script inside the StarterGui and then put your button inside the GUI and then do this

script.Parent.ScreenGui.TextButton.MouseButton1Up:Connect(function)
Frame.Visible = false
Frame2.Visible = false
– and keep on going with your code here and change their names to what you already have

Hope I helped!

Ok I will try to do it. Thanks!

No Problem!
Don’t forget to give me feedback and ask any questions if you have any problem!

Hi,

Theres something wrong with it…

Put an end)
and it needs to be (function()
You need to close the function.