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

How can I open and close a Gui with a key ?

Hi, I want to make a script with them I can open / close a gui with the key q. That´s what I have but I doesn´t work.


-- Variables --


Player = game.Players.LocalPlayer

Mouse = Player:GetMouse()

GUI = game.StarterGui.Handbook.StartMenue

Box = GUI.GameFrame

Open = false


-- Function --


´function PressQ(key)

´ if (key == "q") then

´ if (Open ==false) then

Box.Visible = true

Open = true

elseif (Open == true) then

Box.Visible = false

Open = false

end

end

end

Mouse.Keydown:conncect(PressQ)

1 comment

0

You should be using the UserInputService, not KeyDown.

1 answer

✓ Accepted answer

Instead of using the Player’s mouse, you’re going to use UserInputService, like MCAndRobloxUnited stated.

local UserInputService = game:GetService('UserInputService')
local key = 'Enum.KeyCode.Q'

-- Use local when Global variables aren't needed    
-- Use WaitForChild() on children

local Handbook = game.StarterGui:WaitForChild("Handbook")
local StartMenue = Handbook:WaitForChild("StartMenue")
local GameFrame = StartMenue:WaitForChild("GameFrame")

UserInputService.InputBegan:Connect(function(input)    
	-- Turn the KeyCode Enum into a String    
	-- Match it to our key    
	if tostring(input.KeyCode) == key then    
	-- You can substitute the if statements with the reverse of visible
		GameFrame.Visible = not GameFrame.Visible
	end
end)

2 comments

0

Thank you.

0

doesnt work

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