Trying to index Enums

Is it possible to use [] for Enums, just like what you use for tables.

You usually use these to get values in a table, like so:

local values = {"apple","banana","grapes")

print(value[2])

But does this also work for Enums, supposedly like this:

local keyCode = "W"

if UIS:IsKeyDown(Enum.KeyCode[keyCode]) then
    --whatever
end

If you know, then please comment down. Thank you.

Yes. Indexing using a square bracket with a string is the exact same as indexing with a period.

Would this also work:


local keyCodes = {
    forward = "W",
    backward = "S"
}

Enum.KeyCode[keyCodes[1]]
--First value of keyCodes, meaning:
--Enum.KeyCode["W"]
1 Like

No, you would need to do Enum.KeyCode[keyCodes.forward] not [1] since your table does not have numerical indices.

Okay. Thanks for helping, @7z99.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.