Engine Data Type
Collection
Summary
Code Samples
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.Material = Enum.Material.Plastic
end
-- RunService callback: fires once per heartbeat for each leaf in the collection
function mapleLeaves.OnHeartbeat(leaf: Part, deltaTime: number)
sway(leaf, deltaTime)
end
-- Event callback: connects to BasePart.Touched on each leaf
function mapleLeaves.OnTouched(leaf: Part, other: BasePart)
fall(leaf)
end
-- Property callback: fires when a leaf's Color changes
function mapleLeaves.OnPropertyChanged.Color(leaf: Part)
if leaf.Color == Color3.fromRGB(124, 92, 70) then
fall(leaf)
end
end
-- Set every leaf currently in the collection green
mapleLeaves:ForEach(function(leaf: Part)
leaf.Color = Color3.fromRGB(0, 255, 0)
end)API Reference
Properties
Methods
Destroy
Collection:Destroy()