Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions content/en-us/luau/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ title: Enums
description: 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 of Enums and their items, see [Enums](/reference/engine/enums) in the API Reference.
<Alert severity="info">
Enums are not a [built-in Luau type](https://luau.org/typecheck#builtin-types) and they exist only in Roblox, but they're conceptually similar to other Luau data types and are something you'll work with frequently in Roblox development.
</Alert>

## Get enum items
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](/reference/engine/enums).

To get all items of an Enum, call the `GetEnumItems()` method on the enum. The following code sample demonstrates how to call `GetEnumItems()` on the `Enum.PartType` enum.
## 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.

```lua
local partTypes = Enum.PartType:GetEnumItems()

for index, enumItem in partTypes do
print(enumItem)
end
Expand All @@ -19,39 +24,36 @@ 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 the `Datatype.EnumItem`.
- `Value` - The numerical index of the `Datatype.EnumItem`.
- `EnumType` - The parent `Datatype.Enum` of the `Datatype.EnumItem`.
- `Name` The name of the `Datatype.EnumItem`.
- `Value` The numerical index of the `Datatype.EnumItem`.
- `EnumType` The parent `Datatype.Enum` of the `Datatype.EnumItem`.

Some properties of objects can only be items of certain enums. For example, the `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` 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.

```lua
-- Properties of the EnumItem called Enum.PartType.Cylinder
print(Enum.PartType.Cylinder.Name) -- Cylinder
print(Enum.PartType.Cylinder.Value) -- 2
print(Enum.PartType.Cylinder.EnumType) -- PartType
print(Enum.PartType.Cylinder.Name) --> "Cylinder"
print(Enum.PartType.Cylinder.Value) --> 2
print(Enum.PartType.Cylinder.EnumType) --> PartType
```

## Assign enum items

To assign an `Datatype.EnumItem` as the value of a property, use the full `Datatype.Enum` declaration. You can also use its `Value` or `EnumType`.
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.

```lua
local Workspace = game:GetService("Workspace")

local part = Instance.new("Part") -- Create a new part
part.Shape = Enum.PartType.Cylinder -- By EnumItem (best practice)
part.Shape = Enum.PartType.Cylinder.Value -- By EnumItem Value
part.Shape = 2 -- By EnumItem Value
part.Shape = Enum.PartType.Cylinder.Name -- By EnumItem Name
part.Shape = "Cylinder" -- By EnumItem Name
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
```