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

Trouble getting keyboard input from user using UserInputService?

Asked by 5 years ago

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)

1if input.UserInputType == Enum.UserInputType.Keyboard then
2 
3    local keypressed = input.Keycode
4 
5    print(keypressed)
6 
7end

end)

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Here try this

1function onKeyPress(inputObject, gameProcessedEvent)
2    if inputObject.KeyCode == Enum.KeyCode.F then --change f to the key you want make sure that letter is caps :D--
3print(Player has pressed F)
0
Worked like a charm. Really appreciate the help! Blocktallica 0 — 5y
0
no prob Nervousmrmonkey2 118 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
1userinputservice = game:GetService('UserInputService')
2userinputservice.InputBegan:Connect(function(input,gameProcessed)
3if input.UserInputType == Enum.UserInputType.Keyboard then
4  local keypressed = input.KeyCode
5  print(keypressed)
6end
7end)

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

Answer this question