学习
引擎数据类型
Collection

*该内容即将支持你的所选语言。


概要
方法
ForEach(callback: function)
代码示例
Collection Lifecycle and Events
local CollectionService = game:GetService("CollectionService")
-- A live collection of parts tagged "MapleLeaf" that are direct children of a model tagged "MapleTree"
local mapleLeaves: Collection = CollectionService:CreateCollection("Model.MapleTree > Part.MapleLeaf")
local function sway(leaf: Part, deltaTime: number)
leaf.CFrame *= CFrame.fromEulerAnglesXYZ(0, math.rad(20) * deltaTime, 0)
end
local function fall(leaf: Part)
leaf.Anchored = false
end
-- OnAdded fires once for every leaf already matching, then for each new match
function mapleLeaves.OnAdded(leaf: Part)
leaf.Material = Enum.Material.Neon
end
-- OnRemoved fires when a leaf stops matching (untagged, reparented out of the tree, or destroyed)
function mapleLeaves.OnRemoved(leaf: Part)
leaf:Destroy()
end
-- RunService alias: Fires once per heartbeat for each leaf in the collection
function mapleLeaves.OnHeartbeat(leaf: Part, deltaTime: number)
sway(leaf, deltaTime)
end
-- Instance-event wrapper: OnTouched wraps BasePart.Touched
-- Collection members without a Touched event silently ignore this assignment
function mapleLeaves.OnTouched(leaf: Part, other: BasePart)
fall(leaf)
end
-- Property-changed wrapper: React when a leaf turns brown
function mapleLeaves.OnPropertyChanged.Color(leaf: Part)
if leaf.Color == Color3.fromRGB(124, 92, 70) then
fall(leaf)
end
end
-- Imperatively set every leaf currently in the collection green
mapleLeaves:ForEach(function(leaf: Part)
leaf.Color = Color3.fromRGB(0, 255, 0)
end)

API 参考
属性
Enabled
Collection.Enabled:boolean

方法
ForEach
Collection:ForEach(callback:function)
参数
callback:function

Destroy
Collection:Destroy()

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。