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

Is there an easy way to convert numbers to KeyCodes?

I'd like to put some KeyCodes into a couple of ObjectValue fields, but the best it seems I can do is NumberValues, which don't correspond correctly to Enum.KeyCodes.

I know lua doesn't support bitwise operations or types, but I have my fingers crossed there's a fix for this.

2 comments

0

Do you have any code to tell us what you are trying to do?

0

You could use GetEnumItems like Enum.KeyCode:GetEnumItems()[numberHere]

1 answer

✓ Accepted answer

It's always good practice to use Enums

Part.Material = Enum.Material.Glass
Part.Shape = Enum.PartType.Ball

but it's also possible to use strings, although not recommended (typos and other stuff that will make you cry)

Part.Material = "Glass"
Part.Shape = "Ball"

but the least intuitive of them all are numbers, you can use numbers as well.

Part.Material = 1568
Part.Shape = 0

Why do numbers work? Isn't it odd that we can set properties that are enums, using numbers? It seems like we're pulling numbers out of thin air but actually, these numbers and enums are closely related.


Introducing EnumItem.Value

EnumItems actually have a property called Value which corresponds to that EnumItem's Id. The same goes for KeyCode, KeyCodes have Values as well.

So in order to put KeyCodes into NumberValues, what we can do is not store the KeyCode but rather the Value of the KeyCode

local NumberValue = Instance.new("NumberValue")
NumberValue.Name = "KeyCode"
NumberValue.Parent = script

NumberValue.Changed:Connect(print)

game:GetService("UserInputService").InputBegan:Connect(function(InputObject)
	if InputObject.KeyCode then
		NumberValue.Value = InputObject.KeyCode.Value
	end
end)

1 comment

0

Ah, yes, exactly the answer i was looking for -- now i can just change the value in Explorer -- and a future change input menu can manipulate these values directly

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