Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I open and close guis?

Im new to lua and I need help. Im trying to make a script where if I click something it will open the gui, if its already open then it will close.

How can I get this to work?

game = Game.StarterGui.ScreenGui
game2 = Game.StarterGui.ScreenGui2
open = false


function onButtonClicked()
if open == false then
	game2.Frame.Visible = true
else
	game2.Frame.Visible = false
end
end
script.Parent.MouseButton1Down:connect(onButtonClicked)

4 answers

✓ Accepted answer
player = game.Players.LocalPlayer
game = player.PlayerGui.ScreenGui
game2 = player.PlayerGui.ScreenGui2
open = false
 
 
function onButtonClicked()
if open == false then
    game2.Frame.Visible = true
else
    game2.Frame.Visible = false
end
end
script.Parent.MouseButton1Down:connect(onButtonClicked)


Try this.

game = player.PlayerGui.ScreenGui
game2 = player.PlayerGui.ScreenGui2

function onButtonClicked()
    game2.Frame.Visible = not game2.Frame.Visible --make it the opposite of what it is
end
script.Parent.MouseButton1Down:connect(onButtonClicked)


I personally use boolean toggles for these sorts of things. To make one simply do this:

game = Game.StarterGui.ScreenGui
game2 = Game.StarterGui.ScreenGui2
open = false


function onButtonClicked()
	open = not open;
	if open then
		game2.Frame.Visible = true
	else
		game2.Frame.Visible = false
	end
end
script.Parent.MouseButton1Down:connect(onButtonClicked)

Edit: The other guy had a good idea. I suggest using that.

This discussion is preserved as it was. New answers, comments, and votes are closed.