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

Why isn't userInputService working with a KeyDown?

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

2 comments

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.

0

Could you please post where the currentLetter variable is defined?

1 answer

✓ Accepted answer

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

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