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

Is there an easy way to convert numbers to KeyCodes?

Asked by 5 years ago

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.

0
Do you have any code to tell us what you are trying to do? IFeelLikeAGucciAdlib -2 — 5y
0
You could use GetEnumItems like Enum.KeyCode:GetEnumItems()[numberHere] hiimgoodpack 2009 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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)
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 meistermayo 32 — 5y
Ad

Answer this question