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

Trouble getting keyboard input from user using UserInputService?

Hey, guys, I have just started to get into scripting recently and got stuck for a little while on the UserInputService where I'm trying to get the keyboard input the player presses on their keyboard. I put the local script into StarterPlayerScripts and looked at the API Reference on UserInputService and their code didn't work so I was wondering if anybody could help me out. The error message on the line where i declare the local keypressed variable and says: "KeyCode is not a valid member of InputObject" even though Object Brower has Keycode listed under InputObject. If anyone could help me out if would be a huge help. Thanks! (The following lines starting with 'userinputservice' is my code. I don't know why it doesn't have those in coding lines as well).

userinputservice = game:GetService('UserInputService')

userinputservice.InputBegan:Connect(function(input,gameProcessed)

if input.UserInputType == Enum.UserInputType.Keyboard then

	local keypressed = input.Keycode

	print(keypressed)

end

end)

2 answers

Here try this

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.F then --change f to the key you want make sure that letter is caps :D--
print(Player has pressed F)

2 comments

0

Worked like a charm. Really appreciate the help!

0

no prob

userinputservice = game:GetService('UserInputService')
userinputservice.InputBegan:Connect(function(input,gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
  local keypressed = input.KeyCode
  print(keypressed)
end
end)

KeyCode is caps-sensitive so if you just type Keycode, the game isn't going to recognize it.

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