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

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

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)

3 comments

0

KeyDown is deprecated, use UserInputService.

0

ya know you could just use - script.Parent.ScreenGui.Frame.Visible = not script.Parent.ScreenGui.Frame.Visible

0

so you wont need to use if statements nor booleans

3 answers

✓ Accepted answer

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

1 comment

0

Stop down voting everyone because your answer didn't get accepted.

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

1 comment

0

Yes

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