Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why when I press G it opens the GUI but it wont close after?

Asked by 5 years ago

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)

0
KeyDown is deprecated, use UserInputService. SuedeGod -5 — 5y
0
ya know you could just use - script.Parent.ScreenGui.Frame.Visible = not script.Parent.ScreenGui.Frame.Visible User#23365 30 — 5y
0
so you wont need to use if statements nor booleans User#23365 30 — 5y

3 answers

Log in to vote
-1
Answered by 5 years ago

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)
Ad
Log in to vote
0
Answered by
aazkao 787 Moderation Voter
5 years ago
Edited 5 years ago

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

0
Stop down voting everyone because your answer didn't get accepted. namespace25 594 — 5y
Log in to vote
0
Answered by 5 years ago

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 :)

0
Yes User#19524 175 — 5y

Answer this question