Problem generating a random KeyCode

I am trying to create a minigame which requires users to press random letters on the keyboard, it is all working well when using a concrete letter, although I need to key code to be random.

local UserInputService = game:GetService("UserInputService")

local letterchosen = string.char(math.random(string.byte('A'), string.byte('Z')))

local keycode1 = Enum.KeyCode.letterchosen
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == keycode1 then
		step.Text = "1/5"

I am unaware on how to let the code what to press as “keycode1” is obviously not a valid member of Enum, is there any solutions on how to resolve this issue?

2 Likes

Wheres your InputBegan at?

UserInputService.InputBegan:Connect(function() -- where is it?

Oh sorry that’s in my, code just forgot to miss that out on the message!

Sorry for the late reply, I just found this post while having the same problem.
Changing keycode1 to
Enum.KeyCode[letterchosen]
Should fix the problem.

1 Like

Still very useful to know! Thanks for that!

1 Like