枚數

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

列舉 資料類輸入Enum,是一個固定的項目列表。您可以通過稱為 Enum 的全球對象來存取枚值。要查看完整的枚列和其項目列表,請參閱 枚列 在 API 參考中。

取得枚列項目

要取得枚列的所有項目,請在枚列上呼叫 GetEnumItems() 方法。以下代碼示例顯示如何呼叫 GetEnumItems() 枚列上的 Enum.PartType


local partTypes = Enum.PartType:GetEnumItems()
for index, enumItem in partTypes do
print(enumItem)
end
--[[
Enum.PartType.Ball
Enum.PartType.Block
Enum.PartType.Cylinder
]]

數據類輸入

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

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


-- EnumItem 稱為 Enum.PartType.Cylinder 的屬性
print(Enum.PartType.Cylinder.Name) -- 圓筒
print(Enum.PartType.Cylinder.Value) -- 2
print(Enum.PartType.Cylinder.EnumType) -- PartType

指派枚列項目

要將 EnumItem 指定為屬性性值,請使用完整的 Enum 宣言。您也可以使用其 ValueEnumType


local Workspace = game:GetService("Workspace")
local part = Instance.new("Part") -- 創建新零件
part.Shape = Enum.PartType.Cylinder -- 由 EnumItem (最佳做法)
part.Shape = Enum.PartType.Cylinder.Value -- 由 EnumItem 值
part.Shape = 2 -- 由 EnumItem 值
part.Shape = Enum.PartType.Cylinder.Name -- 依 EnumItem 名稱
part.Shape = "Cylinder" -- 依 EnumItem 名稱
part.Parent = Workspace