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

How do I put variables inside of an enum?

Asked by 2 years ago
Edited 2 years ago

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

Log in to vote
0
Answered by 2 years ago
Enum.KeyCode[inputKey]
Ad
Log in to vote
0
Answered by 2 years ago
local Keys = {
    Enum.KeyCode.Q
}



game:GetService("UserInputService").InputBegan:Connect(function(Input)

    if table.find(Keys, Input.KeyCode) then

        -- // Do stuff

    end

end)

Answer this question