코드 샘플
컬렉션 생명주기 및 이벤트
local CollectionService = game:GetService("CollectionService")
-- "MapleLeaf" 태그가 있는 파트의 실시간 컬렉션으로, "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는 이미 일치하는 각 리프에 대해 한 번 발생하며, 새로운 일치에 대해서도 발생합니다.
function mapleLeaves.OnAdded(leaf: Part)
leaf.Material = Enum.Material.Neon
end
-- OnRemoved는 리프가 일치하지 않을 때(태그가 제거되거나, 나무에서 재부모되거나, 파괴될 때) 발생합니다.
function mapleLeaves.OnRemoved(leaf: Part)
leaf:Destroy()
end
-- RunService 별칭: 컬렉션의 각 리프에 대해 매 심장 박동마다 한 번 발생합니다.
function mapleLeaves.OnHeartbeat(leaf: Part, deltaTime: number)
sway(leaf, deltaTime)
end
-- 인스턴스 이벤트 래퍼: OnTouched는 BasePart.Touched를 래핑합니다.
-- Touched 이벤트가 없는 컬렉션 멤버는 이 할당을 조용히 무시합니다.
function mapleLeaves.OnTouched(leaf: Part, other: BasePart)
fall(leaf)
end
-- 속성 변경 래퍼: 리프가 갈색으로 변할 때 반응합니다.
function mapleLeaves.OnPropertyChanged.Color(leaf: Part)
if leaf.Color == Color3.fromRGB(124, 92, 70) then
fall(leaf)
end
end
-- 현재 컬렉션에 있는 모든 리프를 강제로 초록색으로 설정합니다.
mapleLeaves:ForEach(function(leaf: Part)
leaf.Color = Color3.fromRGB(0, 255, 0)
end)API 참조
속성
메서드
Destroy
Collection:Destroy()