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

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

Asked by 5 years ago

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)

0
You should be using the UserInputService, not KeyDown. DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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)
0
Thank you. pinopeltzi 7 — 5y
0
doesnt work TNTIsLyfe 152 — 4y
Ad

Answer this question