I press G and the GUI opens like it is meant to but when I press G again it doesn't close.
local m = game.Players.LocalPlayer:GetMouse() isOpen = script.Parent.ScreenGui.Frame.Visible m.KeyDown:connect(function(k) k = k:lower() if k == "g" then if isOpen == false then script.Parent.ScreenGui.Frame.Visible = true elseif isOpen == true then script.Parent.ScreenGui.Frame.Visible = false end end end)
Make sure this is in a local script, and next time use UserInputService.
local m = game.Players.LocalPlayer:GetMouse() m.KeyDown:connect(function(k) k = k:lower() if k == "g" then script.Parent.ScreenGui.Frame.Visible = not script.Parent.ScreenGui.Frame.Visible end end)
isOpen is stored as a boolean once at the beginning of your script, it will always stay as false if it is false at the start. You have to read the value at the if statement instead
also use userinputservice, keydown is deprecated, and connect is deprecated, use Connect
local m = game.Players.LocalPlayer:GetMouse() isOpen = script.Parent.ScreenGui.Frame local UIS = game:GetService("UserInputService") UIS.InputBegan:Connect(function(key,gameProcessed) if key.KeyCode == Enum.KeyCode.G then if isOpen.Visible == false then isOpen.Visible = true elseif isOpen.Visible == true then isOpen.Visible = false end end end)
Oh and as expodo said its much simpler to use logical operators, but i didnt do it as this is a good time to learn about how values are only stored once if you declare the variable at the beginning of the script
Really cool answers buts how I does it is by using the not operator and everyone knows it's best way to open and close guis in a single line. Cheers :)