3.7 Enums

Enum,’ short for “enumerate,’” is a predefined data type that contains a list of named constant (read-only) integers.

In Roblox, these are most often used to select settings in classes and objects—not all that different to something like a selector switch for a physical device.

For instance, there is not a ball, cube, cylinder part type in Roblox. There is only one Part object and shape the part takes is determined by its setting.

Lua
local part = game.Workspace.Part

--part.Shape = Enum.PartType.Ball
part.Shape = Enum.PartType.Cylinder
--part.Shape = Enum.PartType.Wedge

This part will take different shapes depending on which setting you uncomment.

These settings is represented by some integer value. If you take a look on the API, you can see that the values for Ball, Block, Cylinder, Wedge, and CornerWedge settings are 0, 1, 2, 3, and 4 respectively.

This means that it is technically possible to set the part shape using just an integer.

Lua
local part = game.Workspace.Part
part.Shape = 3 --Wedge

But humans are bad at memorizing things. And attempting to memorize what setting ‘3’ is will escape you later. So we use enums to represent those integers.


Enums also enforce a context.

PartType is only relevant in the context of Parts. Even if another enum shares an identical integer value, it is prohibited from modifying the setting.

Lua
local part = game.Workspace.Part
part.Shape = Enum.ButtonStyle.RobloxButton --value: 2
--Output: Unable to assign property Shape. EnumItem of type Shape expected, got an EnumItem of type ButtonStyle