2.2 KiB
| title | description |
|---|---|
| Enums | A fixed list of items (enumeration). |
The enumeration data type, or Datatype.Enum, is a fixed list of items. You can access enums through the global object called Datatype.Enum. For a full list and their respective items, see Enums.
Enum items
To get all items of an enum, call the Datatype.Enum:GetEnumItems()|GetEnumItems() method on it. The following code sample demonstrates how to call Datatype.Enum:GetEnumItems()|GetEnumItems() on the Enum.PartType enum.
local partTypes = Enum.PartType:GetEnumItems()
for index, enumItem in partTypes do
print(enumItem)
end
--[[
Enum.PartType.Ball
Enum.PartType.Block
Enum.PartType.Cylinder
Enum.PartType.Wedge
Enum.PartType.CornerWedge
]]
Data type
The Datatype.EnumItem is the data type for items in enums. An Datatype.EnumItem has three properties:
Name— The name of theDatatype.EnumItem.Value— The numerical index of theDatatype.EnumItem.EnumType— The parentDatatype.Enumof theDatatype.EnumItem.
Some properties of objects can only be items of certain enums. For example, the Class.Part.Shape|Shape property of a Class.Part object is an item of the Enum.PartType enum. The following code sample demonstrates how to print the properties of the Enum.PartType.Cylinder enum item.
print(Enum.PartType.Cylinder.Name) --> "Cylinder"
print(Enum.PartType.Cylinder.Value) --> 2
print(Enum.PartType.Cylinder.EnumType) --> PartType
To assign an Datatype.EnumItem as the value of a property, use the full Datatype.Enum declaration. You can also use the item's Datatype.EnumItem.Name|Name property as a string.
local Workspace = game:GetService("Workspace")
local part = Instance.new("Part") -- Create a new part
part.Shape = Enum.PartType.Cylinder -- By full enum item declaration (best practice)
part.Shape = "Cylinder" -- By enum item's name as a string
part.Parent = Workspace