Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 13 additions & 4 deletions content/en-us/luau/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ title: Enums
description: A fixed list of items (enumeration).
---

<Alert severity="info">
Enums are not a [builtin luau type](https://luau.org/typecheck#builtin-types), and exist only in Roblox. This page is located here because Eunms are conceptually similar enough, and are something you will work with all the time in Roblox development.
</Alert>

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.

## Get enum items
Expand All @@ -11,6 +15,7 @@ To get all items of an Enum, call the `GetEnumItems()` method on the enum. The f

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

for index, enumItem in partTypes do
print(enumItem)
end
Expand All @@ -34,15 +39,20 @@ Some properties of objects can only be items of certain enums. For example, the

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

<Alert severity="warning">
Assigning `Datatype.EnumItem` properties like `Class.Part.Shape|Shape` to the `DataType.EnumItem.Value|Value` property is bad practice, as the `DataType.EnumItem.Value|Value` property can be moved around if a new `Datatype.EnumItem` is added to the `Enum.PartType` enum.
Meaning your code may break if you rely on assigning `Datatype.EnumItem` via their `DataType.EnumItem.Value|Value`.
</Alert>

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

Expand All @@ -52,6 +62,5 @@ 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

part.Parent = Workspace
```
4 changes: 2 additions & 2 deletions content/en-us/scripting/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ If you've never written code before and want an introduction to programming, see

## Luau

Roblox scripts use the [Luau](https://luau-lang.org) programming language, which is derived from [Lua 5.1](https://www.lua.org/manual/5.1/).
Roblox scripts use the [Luau](https://luau.org) programming language, which is derived from [Lua 5.1](https://www.lua.org/manual/5.1/).

- Compared to Lua 5.1, Luau adds performance enhancements and many useful features, including an optional typing system, string interpolation, and generalized iteration for tables.
- All valid Lua 5.1 code is valid Luau code, but the opposite is not true.

Most books and online resources for Lua are still broadly applicable to Luau. For a detailed summary of differences, see [Compatibility](https://luau-lang.org/compatibility) in the Luau documentation. For language syntax, see the [Luau reference](../luau/index.md).
Most books and online resources for Lua are still broadly applicable to Luau. For a detailed summary of differences, see [Compatibility](https://luau.org/compatibility) in the Luau documentation. For language syntax, see the [Luau reference](../luau/index.md).

### Luau basics

Expand Down
Loading