How to store KeyCodes?

I’m currently making game settings, and I want for players to be able to change their keybinds. How do I store the keycodes?
Datastore can only accept valid UTF-8 characters and therefore cant simply accept Enum.KeyCode or anything like that.

2 Likes

All EnumItems have a Value number property that’s unique within that Enum group. You can store that value in a datastore. When you load data, get the original enum with Enum.KeyCode[value]

@azavier123 mentioned how to correctly get the KeyCode from that value. I was thinking about something else and got it wrong.

1 Like

You can only index Enum.KeyCode like that if you store the name instead of the value, to load the enum by value there is a function:

Enum.KeyCode:FromValue(val)
1 Like

Enum.Keycode has a Name property, perhaps you can save the name to the data store then do something like Enum.Keycode[storedKey]

I didn’t know there was a function to get the Keycode from Value (whoops)

1 Like

You can store the name or the number. Its like KeyCode.name or something like that, thats exactly what I did for my custom keybinds system. When the game loads in create a function that decodes those back into the proper enums

1 Like

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