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

What is the event for when someone pushes a button?

Asked by 6 years ago

I'm stuck on how to find when a player presses a button like "G" or anything

2 answers

Log in to vote
1
Answered by 6 years ago

We need to use ContextActionService For input or even UserInputService

local cas = game:GetService("ContextActionService")
function onKeyPress(actionName, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then -- gets the input
        print("R was pressed")
    end
end

cas:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.R)

On the final line of code It does the function when you press R on your keyboard You can change Enum.KeyCode.R to What key you want(ex. Enum.KeyCode.Z)

Ad
Log in to vote
0
Answered by
oSyM8V3N 429 Moderation Voter
6 years ago
Edited 6 years ago

You can use UserInputService.

For ex you can write :

local UIS = game:GetService("UserInputService")

UIS.InputBegan:connect(function(Input)
local KeyCode = Input.KeyCode

if KeyCode == Enum.KeyCode.G then
print("G was pressed")
end)
0
Let me know if this helped :) oSyM8V3N 429 — 6y
0
This didn't work, on then it says it must have identifier KennySfromTitan 106 — 6y
0
Oh sorry i forgot to add a if. It should work now oSyM8V3N 429 — 6y

Answer this question