PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Using Enumerations

Using Enumerations

Jun 23 2018, 2:02 AM PST

Problem

You want to use enumerations.

Solution

Use the Enum table.

Workspace.Part.Shape = Enum.PartType.Ball

Discussion

You may have noticed an interesting phenomenon. Take this code for example.

local part = Instance.new('Part')
part.Shape = 'Ball'
part.Parent = Workspace
print(part.Shape == 'Ball') --> false

Why is the Shape of the part not a “Ball?” We clearly set it to be a “Ball” and if you look in the Workspace you will see a ball. This is because part.Shape is not a string, it is an enumeration. An enumeration, or enum for short, is a strict list of values for a property. There are only 3 types of shapes that a part can be, a Ball, Block or Cylinder. These are part of the PartType enum accessible by Enum.PartType.

Why would we ever need to use Enum when we can use the shortcut of passing the string of the name of the enum? For one: its good practice to explicitly state that what you’re setting is an enumeration value as it serves for better reading code. You must use enumerations if you are checking an enumeration value. If you use the shortcut notation, the property itself is automatically transformed (or more accurately stated coerced) into an enumeration value. Here is an example.

local part = Instance.new('Part')
part.Shape = 'Ball'
part.Parent = Workspace
print(part.Shape == Enum.PartType.Ball) --> true
Tags:
  • data-type