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

How do I make a keypress listener? [Solved]

I am having a bit of difficulty with checking for keypresses, I have some code that is supposed to check for a keypress every tick, but I am not sure how to check for the actual keypress:

function onKeyPress(actionName, userInputState, inputObject)
		print("R has been pressed")
end

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

runService.RenderStepped:connect(function()  -- update every 1/60 of a second
	if [not sure what to do here] then
		--do something
	end
end)

Also a mini question here, is there any way to program everything instead of using small scripts everywhere?

1 answer

✓ Accepted answer

no need for while true do do this

local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(Input, IsTyping)

if Input.KeyCode == Enum.KeyCode.R and not IsTyping then

-- do stuff
	end
end)

4 comments

0

Can you explain the "IsTyping" part?

0

It is the second parameter given when InputBegan is fired. It indicates if player input was caused due to them interacting with UI. The common parameter name is 'gameProcessed' and is not only for checking if a player is typing in chat.

0

So forgot to ask one last thing, how will this know who is typing?

0

Never mind, ignore that, I was just being stupid and didn't realize this was supposed to be in playerscripts

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