CollectionService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
CollectionService จัดการกลุ่ม (คอลเลกชัน) ของอินสแตนซ์ที่มี แท็ก การใช้งาน Tags เป็นชุดสตริงที่ใช้กับวัตถุที่เรียนรู้จากเซิร์ฟเวอร์ไปยังเครื่องคอมพิวเตอร์ พวกเขายังได้รับการเ
การใช้งานหลักของ CollectionService คือการลงทะเบียนติดตั้งตัวอย่างที่มีแท็กที่กำหนดเฉพาะที่คุณสามารถใช้เพื่อขยายพฤติกรรมของพวกเขา หากคุณพบว่าตัวเองกำลังเพิ่มสคริปต์เด
แท็กสามารถเพิ่มหรือลบผ่านวิธีการของคลาสนี้เช่น AddTag() หรือ RemoveTag() พวกเขายังสามารถจัดการโดยตรงใน Studio ผ่าน
การเลียนแบบ
เมื่อแท็กเรียกซ้ำกัน แท็กทั้งหมดบนวัตถุจะเรียกซ้ำในเวล
สรุป
วิธีการ
ใช้ป้ายชื่อให้กับ Instance
กลับสัญญาณที่เปิดเมื่อมีการเพิ่มแท็กให้กับวัตถุ
กลับสัญญาณที่เปิดใช้งานเมื่อป้ายชื่อที่กำหนดถูกลบออกจากตัวอินสแตนซ์
กลับรายการของวัตถุในเกมด้วยแท็กที่กำหนด
รับรายการทั้งหมดที่ใช้กับวัตถุที่กำหนด
ตรวจสอบว่าวัตถุมีแท็กที่กำหนด
ลบแท็กออกจากอินสแตนซ์
อีเวนต์
จะเริ่มไฟร์เมื่อมีการเพิ่มแท็กไปยังวัตถุและแท็กที่เพิ่มมีเพียงอยู่ในสถานที่เดียว
จะเกิดไฟไหม้เมื่อป้ายชื่อถูกลบออกจากวัตถุและป้ายชื่อนี้ไม่ได้ถูกใช้อีกในสถานที่ใด
คุณสมบัติ
วิธีการ
AddTag
AddTag จะใช้แท็กไปยัง Instance โดยไม่ทำอะไรถ้าแท็กนี้ถูกใช้กับตัวอินสแตนซ์ การเพิ่มแท็กจะเรียกสัญญาณที่สร้างขึ้นโดย CollectionService:GetInstanceAddedSignal() ด้วยแท็กที่ให้
คำเตือน: เมื่อมีการแท็กอินสแน็ป มักจะมีการใช้ทรัพยากรบางอย่างเพื่อให้ใช้งานของแท็ก
พารามิเตอร์
ส่งค่ากลับ
GetInstanceAddedSignal
GetInstanceAdded ได้รับแท็ก (สตริง) และส่งสัญญาณที่ไฟลุกซ์เปิดในสองเงื่อนไข:
- ป้ายชื่อถูกกำหนดให้กับติดตั้งภายใน DataModel (เกม) โดยใช้ CollectionService:AddTag() หรือ Instance:AddTag()
- ตัวอินสแตนซ์ที่มีป้ายชื่อที่กำหนดจะถูกเพิ่มเป็นลูกของ DataModel โดยการตั้งค่า Instance.Parent หรือสิ่งที่คล้ายกัน
การโทรที่ติดตามมีเดียวกันในวิธีนี้จะส่งคืนเดียวกับสัญญาณเหมือนกัน โปรดพิจารณาการโทร CollectionService:GetTagged() เพื่อรับรายการของวัตถุที่มีแท็กแล้ว (และดังนั้นจึงไม่ส
ดูเพิ่มเติม CollectionService:GetInstanceRemovedSignal() ซึ่งกลับสัญญาณเหตุการณ์ที่เกิดขึ้นภายใต้เงื่อนไขที่คล้ายกัน
พารามิเตอร์
แท็กที่จะดู
ส่งค่ากลับ
เหตุการณ์ที่เกิดขึ้นเมื่อคุณเพิ่มป้ายชื่อให้กับตัวอินสแตนซ์
ตัวอย่างโค้ด
This code sample causes any BasePart with the tag Deadly to kill any Humanoid that touches it. It does this using a common pattern with CollectionService to listen for all parts with the tag and make a connection, then disconnect the connection when the tag is removed.
local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
local connections = {}
local function onInstanceAdded(object)
-- Confirm that the object with this tag is a BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- If there is a stored connection on this object, disconnect/remove it
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end
GetInstanceRemovedSignal
GetInstanceRemoved ได้รับแท็ก (สตริง) และส่งสัญญาณที่ไฟลุกซ์ต้องเปิดในสองเงื่อนไข:
- ป้ายชื่อจะถูกลบออกจากเซิร์ฟเวอร์ภายใน DataModel (เกม) โดยใช้ CollectionService:RemoveTag() หรือ Instance:RemoveTag()
- ตัวอย่างที่มีป้ายชื่อที่ให้ไว้จะถูกลบออกเป็นลูกหลานของ DataModel เช่นโดยการยกเลิกการตั้งค่า Instance.Parent หรือคุณสมบัติที่คล้ายคลึงกัน
การเรียกต่อไปนี้ในวิธีนี้ด้วยแท็กเดียวกันจะส่งคืนไอเท็มสัญญาณที่เหมือนกัน สัญญาณเป็นประโยชน์สำหรับการทำความสะอาดทรัพยากรที่ใช้โดยวัตถุที่เคยมีแท็ก เช่น การตัดการเชื่อมต่อ
ดูเพิ่มเติม CollectionService:GetInstanceAddedSignal() ซึ่งเป็นตัวแทนของเหตุการณ์ที่เกิดขึ้นภายใต้เงื่อนไขที่คล้ายกัน
พารามิเตอร์
แท็กที่จะดู
ส่งค่ากลับ
เหตุการณ์ที่เกิดขึ้นเมื่อคุณลบแท็กออกจากอินสแตนซ์
ตัวอย่างโค้ด
This code sample causes any BasePart with the tag Deadly to kill any Humanoid that touches it. It does this using a common pattern with CollectionService to listen for all parts with the tag and make a connection, then disconnect the connection when the tag is removed.
local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
local connections = {}
local function onInstanceAdded(object)
-- Confirm that the object with this tag is a BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- If there is a stored connection on this object, disconnect/remove it
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end
GetTagged
GetTagged กลับรายการอาวุธที่มีแท็กที่กำหนดซึ่งเป็นบุตรหลานของ DataModel ( game เช
หากคุณต้องการตรวจจับวัตถุทั้งหมดด้วยแท็กทั้งปัจจุบันและอนาคตใช้วิธีนี้เพื่อทวีคูณวัตถุในขณะที่กำลังสร้างการเชื่อมต่อกับสัญญาณที่กลับโดย CollectionService.GetInstanceAddedSignal
วิธีนี้ไม่รับประกันคำสั่งซื้อของวัตถุที่กลับมา นอกจากนี้ยังเป็นไปได้ว่าวัตถุอาจมีป้ายชื่อที่กำหนดไว้แล้ว แต่ไม่ใช่ลูกหลานของ DataModel นั่นคือ วิธีนี้จะไม่ส่งคืน
พารามิเตอร์
ป้ายที่ค้นหา
ส่งค่ากลับ
รายการของทุกรุ่นที่มีแท็ก
ตัวอย่างโค้ด
This code sample causes any BasePart with the tag Deadly to kill any Humanoid that touches it. It does this using a common pattern with CollectionService to listen for all parts with the tag and make a connection, then disconnect the connection when the tag is removed.
local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
local connections = {}
local function onInstanceAdded(object)
-- Confirm that the object with this tag is a BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- If there is a stored connection on this object, disconnect/remove it
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end
GetTags
GetTags ได้รับตัวอินสแตนซ์และส่งคืนรายการสตริงที่เป็นเครื่องหมายที่ใช้กับตัวองค์ประกอบที่ให้
local CollectionService = game:GetService("CollectionService")local object = workspace.Modellocal tags = CollectionService:GetTags(object)print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))
วิธีนี้มีประโยชน์เมื่อคุณต้องการทำอะไรบางอย่างด้วยหลายแท็กในหนึ่งวัตถุ อย่างไรก็ตาม จะเป็นการใช้เวลามากที่จะใช้วิธีนี้เพื่อตรวจสอบความถูกต้องของแท็กเดียว สำหรับสิ่
พารามิเตอร์
เป็นวัตถุที่มีแท็กที่ต้องการจะถูกส่งกลับ
ส่งค่ากลับ
รายการสตริงที่ใช้เป็นแท็กกับวัตถุที่กำหนด
ตัวอย่างโค้ด
This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Add a tag
CollectionService:AddTag(object, "Deadly")
-- Query for a tag
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " is deadly")
end
-- List tags on an object
local tags = CollectionService:GetTags(object)
print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))
-- Remove a tag
CollectionService:RemoveTag(object, "Deadly")
HasTag
HasTag กลับว่ามีแท็กใด ๆ บนวัตถุที่ให้หรือไม่
- การใช้ CollectionService:AddTag() เพื่อเพิ่มป้ายชื่อจะทำให้วิธีนี้กลับมาเป็นตัวเลือกที่ถูกต้อง
- การใช้ CollectionService:RemoveTag() เพื่อลบแท็กจะทำให้วิธีนี้กลับไปที่ false
โดยการขยายตัวใด ๆ ใด ๆ ที่ป้อนโดยการโทรไปยัง CollectionService:GetTags() บนตัวอุปกรณ์จะกลับมาเป็นจริงเมื่อใช้วิธีนี้
พารามิเตอร์
ส่งค่ากลับ
ว่ามีแท็กในตัวอินสแตนซ์หรือไม่
ตัวอย่างโค้ด
This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Add a tag
CollectionService:AddTag(object, "Deadly")
-- Query for a tag
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " is deadly")
end
-- List tags on an object
local tags = CollectionService:GetTags(object)
print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))
-- Remove a tag
CollectionService:RemoveTag(object, "Deadly")
RemoveTag
การลบแท็กจะลบแท็กออกจากอินสแตนซ์ วิธีนี้จะไม่สร้างข้อผิดพลาดหากวัตถุไม่มีแท็กในสถานที่แรก การลบแท็กออกจะเปิดเปิดสัญญาณที่สร้างโดย CollectionService:GetInstanceRemovedSignal() ด้วยแท็กที่ให้
เมื่อลบแท็ก มักจะเป็นเรื่องปกติที่จะใช้ทรัพยากรบางอย่างเพื่อให้ความสามารถของแท็ก เช่น การเชื่อมต่อเหตุการณ์หรือตาราง เพื่อป้องกันการหลุดหาย จึงเป็นเรื่องดีที่จะทำความสะอ
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Add a tag
CollectionService:AddTag(object, "Deadly")
-- Query for a tag
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " is deadly")
end
-- List tags on an object
local tags = CollectionService:GetTags(object)
print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))
-- Remove a tag
CollectionService:RemoveTag(object, "Deadly")