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

Why isn't userInputService working with a KeyDown?

Asked by 5 years ago

I have a variable called 'currentLetter' which is used earlier on in my code to get the first letter of string. I also have a textbox which I want to detect whether or not the letter I have typed is in the variable. I have done this with userInputService and using InputBegan but it doesn't seem to be working.

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == currentLetter then
        print("Hello")
    end
end)

Any help would be very much appreciated. Thanks

0
Keycode's are meant to be used as Enumerators. Any old string value will never work. Aditionally no point in adding gameproccesedevent argument if you aren't using it. Gojinhan 353 — 5y
0
Could you please post where the currentLetter variable is defined? incapaz 195 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

The inputObject isn't a letter/string. It is an Enum. String keys were deprecated a long time ago.

The enum for keyboard keys are Enum.inputObject.KeyCode."INSERT KEY HERE"

Here is your script fixed:

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.B then
        print("Hello")
    end
end)

Note: I put the key B just to fill in the script, however, you can change it to your own liking like Enum.KeyCode.F and so on. I also swapped :Connect() with :connect() because :connect is deprecated and not supported.

Here's a link to the Enum keycode: https://developer.roblox.com/en-us/api-reference/enum/KeyCode

Ad

Answer this question