列舉

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

列舉數據類型,或 Enum,是一個固定的項目列表。您可以通過名為 Enum 的全局對象訪問列舉。要查看完整列表及其各自的項目,請參閱 列舉

列舉項目

要獲取某個列舉的所有項目,請在其上調用 GetEnumItems() 方法。以下代碼示例演示如何在 Enum.PartType 列舉上調用 GetEnumItems()


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
]]

數據類型

EnumItem 是列舉中項目的數據類型。一個 EnumItem 具有三個屬性:

某些對象的屬性只能是某些列舉的項目。例如,Shape 屬性是一個 Part 對象的 Enum.PartType 列舉的項目。以下代碼示例演示如何打印 Enum.PartType.Cylinder 列舉項目的屬性。


print(Enum.PartType.Cylinder.Name) --> "Cylinder"
print(Enum.PartType.Cylinder.Value) --> 2
print(Enum.PartType.Cylinder.EnumType) --> PartType

要將 EnumItem 指定為屬性的值,請使用完整的 Enum 聲明。您也可以將項目的 Name 屬性作為字符串使用。


local Workspace = game:GetService("Workspace")
local part = Instance.new("Part") -- 創建一個新的部件
part.Shape = Enum.PartType.Cylinder -- 通過完整的列舉項目聲明(最佳實踐)
part.Shape = "Cylinder" -- 通過列舉項目的名稱作為字符串
part.Parent = Workspace
©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。