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.
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.
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.
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
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)
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.