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

How can I run a script when a key is pressed ?

Asked by 5 years ago
Edited 5 years ago

Hi, I want to make a script which runs when a Key is pressed down. How can I make it ? I have this :

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
Just make a function. MEndermanM 73 — 5y
0
what is this question? this should work what are you asking? starmaq 1290 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

Do not make an Enum a string, as there is no point in doing so. You can simply use the input parameter like so (Local Script):

local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input, GPE)
    if not GPE then
        if input.KeyCode == Enum.KeyCode.Q then
            -- Do something
        end
    end
end)

You can simply check if the KeyCode is equal to an Enum. There is absolutely no point in making it a string.

0
local Handbook = game.StarterGui.Handbook.StartMenue.GameFrame local UIS = game:GetService("UserInputService") UIS.InputBegan pinopeltzi 7 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

What did I wrong ?

local Handbook = game.StarterGui.Handbook.StartMenue.GameFrame
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, GPE)
    if not GPE then
        if input.KeyCode == Enum.KeyCode.H then
            Handbook.Visible = true
            end
        end
    end
end)
0
Access Handbook via the LocalPlayer's PlayerGui. DeceptiveCaster 3761 — 5y

Answer this question