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

How would i set the Input.KeyCode's value to a StringValue's value?

I have a function that chose's a random key from a table containing keys like : Q, E, R, T, Y, etc. Then i made this other function using UIS:

UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
	local KeyCode = Input.KeyCode
	local keyPress = sgui:FindFirstChild("Menu").status.KPositioner.keyValue.Value
	if not gameProcessedEvent then
		
		if KeyCode == Enum.KeyCode.keyPress.Value then
			print("WEWE")
		end
	end
end)

But when i wrote if KeyCode == Enum.KeyCode.keyPress.Value then, it outputs an error. Since the keyPress's Value is a random key from the table, how would i make it so the KeyCode depends on the keyPress's Value?

1 answer

✓ Accepted answer

You should know that there are two index operators (which is basically what you do with Enums). The two operators are . and []. Using the dot, it's always going to look with something with that name. Using the brackets, you can input a string or a variable to look for something inside. To easily fix this you do

if KeyCode == Enum.KeyCode[keyPress] then
	print("WEWE")
end

2 comments

0

It still shows the Players.YBN_oSy.PlayerScripts.keydownthing:110: bad argument #2 to '?' (string expected, got nil) error

0

if KeyCode == Enum.KeyCode[tostring(keyPress)] then

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