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

What does Enum mean to Keycodes, and does it only used for Keycodes?

Asked by 5 years ago
Edited 5 years ago

So, I still don't know how Enum functions, but there are commonly used for UINS. So my friend use it instead of KeyDown, so here it is? What does it mean? Sample:

UINS.InputEnded:connect(function(key)
    if key.KeyCode == !Enum!.KeyCode.E then

The word highlighted with 2 exclamation marks.

0
The exclamation mark shouldn’t be there. User#19524 175 — 5y
0
I was about to highlight it cherrythetree 130 — 5y
0
The second unaccepted answer might better explain what you are looking for. mattscy 3725 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

Definition of Enum from ROBLOX Dev:

An “Enumeration”, or “Enum”, is a special data type that can take one of a set of values. For example, the SurfaceType enum is used to select what type of surface a brick has on a certain side, which can take the values “Glue”, “Hinge”, “Inlet”, or a few more. These values act as constants in place of using ambiguous numbers or strings to set a value. You can use the Object Browser in Studio to see a list of all of the enums.

http://robloxdev.com/articles/Enumeration

Ad
Log in to vote
1
Answered by
amanda 1059 Moderation Voter
5 years ago
Edited 5 years ago

Enumeration

An “Enumeration”, or “Enum”, is a special data type that can take one of a set of values. What this means within ROBLOX, is there are set options that certain properties' values can be equal to, and these are enforced with the Enum data type.

The reason you may have missed a lesson on Enums, is because they can be assigned with strings as an overload. The reason you are confronting them now, is because to compare Enums you must write them out.

Material

One common example of an Enumeration is with the Material property of a BasePart.

Lets say you inserted a Part into the workspace, and wanted to see what it's Material was.

print(workspace.Part.Material) --> Enum.Material.Plastic

Ahah! However, as I said previously, you can assign it either by it's Enum, or with a string as apart of an internal overload function

local part = workspace.Part

part.Material = "Grass"

print(part.Material == "Grass") --> false
print(part.Material == Enum.Material.Grass) --> true

part.Material = Enum.Material.DiamondPlate

KeyCode

Using your example, most people have to confront what an Enum is when they start using UserInputService, as there is a defined number of valid keys that can be pressed, so ROBLOX made an Enum for it.

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(obj)
    print(obj.KeyCode) --> Enum.KeyCode.G
    if obj.KeyCode == Enum.KeyCode.G then
        print("You are a G!") --> You are a G!
    end
end)

Conclusion

EnumItems are NOT strings, and when comparing them to anything you should take note their true value, otherwise you may find yourself stuck in a conditional statement.

They are useful for declaring a set number of options for a certain type of value.

Sources/Links

Answer this question