เรียนรู้
ประเภทข้อมูลของ Engine
Collection

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่


สรุป
คุณสมบัติ
วิธีการ
ForEach(callback: function)
ตัวอย่างโค้ด
วงจรชีวิตและเหตุการณ์ของการเก็บรวบรวม
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 alias: จะถูกเรียกครั้งต่อครั้งในแต่ละการเต้นของหัวใจสำหรับใบแต่ละใบในคอลเลกชัน
function mapleLeaves.OnHeartbeat(leaf: Part, deltaTime: number)
sway(leaf, deltaTime)
end
-- Instance-event wrapper: OnTouched ห่อหุ้ม BasePart.Touched
-- สมาชิกของคอลเลกชันที่ไม่มีเหตุการณ์ Touched จะเงียบ ๆ มองข้ามการมอบหมายนี้
function mapleLeaves.OnTouched(leaf: Part, other: BasePart)
fall(leaf)
end
-- Property-changed wrapper: ตอบสนองเมื่อใบเปลี่ยนเป็นสีน้ำตาล
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
คุณสมบัติ
Enabled
Collection.Enabled:boolean

วิธีการ
ForEach
Collection:ForEach(callback:function)
พารามิเตอร์
callback:function

Destroy
Collection:Destroy()

©2026 Roblox Corporation. Roblox, โลโก้ Roblox และ Powering Imagination เป็นส่วนหนึ่งของเครื่องหมายการค้าที่จดทะเบียน และไม่ได้จดทะเบียนของเราในสหรัฐฯ และประเทศอื่นๆ