(SOLVED) How to make button gui open and close when pressing the same button

  1. What do you want to achieve? Button that opens a gui whilst being able to click on it to open the gui and close the gui

  2. **What is the issue?**d i want a different type of button that opens and closes in the same button but i am bad at scripting/new

  3. What solutions have you tried so far? Looked on yt and dev fourm but i cant find anything

script.Parent.MouseButton1Click:Connect(function()
	game.StarterGui.ScreenGui.Frame.Visible = true
end)
8 Likes

Could you show your script’s code?

2 Likes

Yes i will hold on ill edit it

Do not have the button a child of the frame you are wanting to toggle.

To make a button be able to make something both visible and invisible, you’ll just have to change your MouseButton1Click function slightly.

Frame = script.Parent.Parent.Frame
script.Parent.MouseButton1Click:Connect(function()
	Frame.Visible = not Frame.Visible
end)

And as @SelDraken said, do not have the button parented to the frame if youre going to change its visibility.
This will cause the button to become invisible when changing the frames visibility:
Screenshot (487)

This will not cause the button to become invisible when changing the frames visibility:
Screenshot (488)

3 Likes

i edited to my new script that i think will work for opening it

but i want it so the same button opens and closes it

The script i supplied does make the same button open and close it.
The whole reason i did not Frame.Visible is because it inverts the BoolValue of the Visible property.
So if Frame.Visible == true then it will make it false, and if Frame.Visible == false then it will make it true.

2 Likes

will this work?

local Frame = game.StarterGui.ScreenGui.Frame.Visible
if Frame == true then
	
	script.Parent.MouseButton1Click:Connect(function()
	Frame = false
	end)
	
end
if Frame == false then
	
	script.Parent.MouseButton1Click:Connect(function()
		Frame = true
	end)
	
end

There is a lot of pointless code there,

Replace your entire script with this:

-- Im assuming that the button is parented to the same ScreenGui
-- If it isnt, make it parented to the same ScreenGui, NOT THE SAME FRAME.
local Frame = script.Parent.Frame
script.Parent.MouseButton1Click:Connect(function()
Frame.Visible = not Frame.Visible
end
2 Likes

wait my button is no longer in the frame object though?
image

Maybe the more verbose version would be better for @DuckKing_FRS to understand it.

1 Like

Yes, that would work. just parent the script to the ScreenGui itself, and not the button.
And then instead of doing
script.Parent.MouseButton1Click ,
do
Script.Parent.account_box.MouseButton1Click

local switch = true
script.Parent.MouseButton1Up:Connect(function()
    if switch then
        game.StarterGui.ScreenGui.Frame.Visible = true
        switch = false
    else
        game.StarterGui.ScreenGui.Frame.Visible = false
        switch = true
    end
end)
local Frame = game.StarterGui.ScreenGui.Frame.Visible
script.Parent.MouseButton1Click:Connect(function()
if Frame.Visible == true then
  Frame.Visible = false
else
	Frame.Visible = true
end
	
end)

Heres a video of me setting up what i think you want, and what im trying to describe.

4 Likes

image
heres what i have i just dont know how to unlock mouse so i can press it cuz my game is first person

change the
image
to be just an
else

i tried your code but i just gave me error
image

you are missing a ) after the last end

end)