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

How do I put variables inside of an enum?

I'm going to explain this the best I can without knowing the correct terminology for it;

I have been trying to figure out how I can allow the player to toggle something by inputting their own key-bind for it.

This is what I have been trying to do so far.

	inputKey = ("Q")

	local userInputService = game:GetService("UserInputService")
	userInputService.InputBegan:Connect(function(input,gameProcessedEvent)
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.. tostring(inputKey) then
				-- do stuff

The problem here is the Enum.KeyCode.. tostring(inputKey) I have no idea how to merge these two together.

The code works by itself. If you were to switch out the .. tostring(inputKey) with, for example, the letter Q, it does what I want.

I have a feeling that there is a simple fix for this. I am very close to figuring this out, and I would love it if anybody that knows how to fix this would be able to tell me!

Thank you in advance!

2 answers

local Keys = {
	Enum.KeyCode.Q
}



game:GetService("UserInputService").InputBegan:Connect(function(Input)
	
	if table.find(Keys, Input.KeyCode) then
		
		-- // Do stuff
		
	end
	
end)

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