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

What is the event for when someone pushes a button?

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

2 answers

✓ Accepted answer

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)

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)

3 comments

0

Let me know if this helped :)

0

This didn't work, on then it says it must have identifier

0

Oh sorry i forgot to add a if. It should work now

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