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 4 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)

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

    local keypressed = input.Keycode

    print(keypressed)

end

end)

2 answers

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

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)
0
Worked like a charm. Really appreciate the help! Blocktallica 0 — 4y
0
no prob Nervousmrmonkey2 118 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
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.

Answer this question