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?
01 | game = Game.StarterGui.ScreenGui |
02 | game 2 = Game.StarterGui.ScreenGui 2 |
03 | open = false |
04 |
05 |
06 | function onButtonClicked() |
07 | if open = = false then |
08 | game 2. Frame.Visible = true |
09 | else |
10 | game 2. Frame.Visible = false |
11 | end |
12 | end |
13 | script.Parent.MouseButton 1 Down:connect(onButtonClicked) |
01 | player = game.Players.LocalPlayer |
02 | game = player.PlayerGui.ScreenGui |
03 | game 2 = player.PlayerGui.ScreenGui 2 |
04 | open = false |
05 |
06 |
07 | function onButtonClicked() |
08 | if open = = false then |
09 | game 2. Frame.Visible = true |
10 | else |
11 | game 2. Frame.Visible = false |
12 | end |
13 | end |
14 | script.Parent.MouseButton 1 Down:connect(onButtonClicked) |
Try this.
1 | game = player.PlayerGui.ScreenGui |
2 | game 2 = player.PlayerGui.ScreenGui 2 |
3 |
4 | function onButtonClicked() |
5 | game 2. Frame.Visible = not game 2. Frame.Visible --make it the opposite of what it is |
6 | end |
7 | script.Parent.MouseButton 1 Down:connect(onButtonClicked) |
I personally use boolean toggles for these sorts of things. To make one simply do this:
01 | game = Game.StarterGui.ScreenGui |
02 | game 2 = Game.StarterGui.ScreenGui 2 |
03 | open = false |
04 |
05 |
06 | function onButtonClicked() |
07 | open = not open; |
08 | if open then |
09 | game 2. Frame.Visible = true |
10 | else |
11 | game 2. Frame.Visible = false |
12 | end |
13 | end |
14 | script.Parent.MouseButton 1 Down:connect(onButtonClicked) |
Edit: The other guy had a good idea. I suggest using that.