CollectionService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
CollectionService จัดการกลุ่ม (คอลเลกชัน) ของตัวอย่างที่มีแท็ก **** แท็กคือชุดของสตริงที่ใช้กับตัวอย่างที่สําเร็จจากเซิร์ฟเวอร์ไปยังไคลเอนต์พวกเขายังถูกเซริซิสเมื่อสถานที่ถูกบันทึก
การใช้งานหลักของ CollectionService คือการลงทะเบียนตัวอย่างที่มีแท็กเฉพาะที่คุณสามารถใช้เพื่อขยายพฤติกรรมได้หากคุณพบว่าตัวเองเพิ่มสคริปต์เดียวกันไปยังหลายตัวอย่างที่แตกต่างกัน สคริปต์ที่ใช้ CollectionService อาจจะดีกว่า
แท็กสามารถเพิ่มหรือลบผ่านวิธีการของคลาสนี้เช่น AddTag() หรือ RemoveTag()พวกเขายังสามารถจัดการได้โดยตรงใน Studio ผ่านส่วน แท็ก อินสแตนซ์เอเดอร์แท็กที่สร้างขึ้น
การเลียนแบบ
เมื่อแท็กซ้ำ แท็กทั้งหมดในตัวอย่างจะซ้ำในเวลาเดียวกัน ดังนั้นหากคุณตั้งแท็กบนตัวอย่างจากไคลเอนต์แล้วเพิ่ม/ลบแท็ก ที่แตกต่าง บนตัวอย่างเดียวกันจากเซิร์ฟเวอร์แท็กท้องถิ่นบนตัวอย่างจะถูกเขียนทับใน StreamingEnabled สถานที่ ตัวอย่างสามารถถอดออกได้เมื่อพวกเขาออกจากพื้นที่สตรีมของไคลเอนต์หากตัวอย่างดังกล่าวเข้าสู่พื้นที่สตรีมอีกครั้ง คุณสมบัติและแท็กจะถูกซิงโครไลซ์ใหม่จากเซิร์ฟเวอร์สิ่งนี้อาจทำให้การเปลี่ยนแปลงที่ทำโดย LocalScripts ถูกเขียนทับ/ลบ
สรุป
วิธีการ
ใช้แท็กกับ Instance ที่
คืนค่าเป็นคอลเลกชันของแท็กทั้งหมดในประสบการณ์
ส่งสัญญาณที่จะยิงเมื่อแท็กที่กำหนดถูกเพิ่มลงในตัวอย่าง
ส่งสัญญาณที่จะยิงเมื่อแท็กที่กำหนดถูกลบออกจากตัวอย่าง
คืนค่าเป็นคอลเลกชันของตัวอย่างในเกมด้วยแท็กที่กำหนดไว้
รับคอลเลกชันของแท็กทั้งหมดที่ใช้กับตัวอย่างที่กำหนด
ตรวจสอบว่าตัวอย่างมีแท็กที่กำหนดหรือไม่
ลบแท็กจากตัวอย่าง
อีเวนต์
ไฟเมื่อแท็กถูกเพิ่มลงในตัวอย่างและแท็กที่เพิ่มมาเป็นการปรากฏเดียวของแท็กนั้นในสถานที่
จะเกิดไฟไหม้เมื่อแท็กถูกลบออกจากตัวอย่างและแท็กที่ถูกลบไม่ถูกใช้ที่ใดในสถานที่อีกต่อไป
คุณสมบัติ
วิธีการ
AddTag
วิธีนี้ใช้แท็กกับ Instance โดยไม่ทำอะไรถ้าแท็กถูกใช้กับตัวอย่างนั้นแล้วการเพิ่มแท็กสําเร็จจะส่งสัญญาณที่สร้างขึ้นโดย GetInstanceAddedSignal() ด้วยแท็กที่กำหนดไว้
คําเตือน
แท็กของตัวอย่างที่ถูกเพิ่มบนเครื่องคอมพิวเตอร์จะถูกลบหากเซิร์ฟเวอร์เพิ่มหรือลบแท็กบนตัวอย่างนั้นในภายหลังเพราะเซิร์ฟเวอร์สำเนาทั้งหมดแท็กและเขียนทับแท็กก่อนหน้า
อินสแตนซ์มันเป็นเรื่องธรรมดาที่ทรัพยากรบางอย่างจะถูกใช้เพื่อให้ป้ายกำกับมีฟังก์ชัน เช่น การเชื่อมต่อกิจกรรมหรือตารางเพื่อป้องกันการรั่วไหลของหน่วยความจํา เป็นความคิดที่ดีที่จะทําความสะอาดพวกเขา (ตัดการเชื่อมต่อ, ตั้งค่าเป็น nil เป็นต้น) เมื่อไม่จําเป็นต้องใช้แท็กอีกต่อไปทำเช่นนี้เมื่อเรียก RemoveTag() , เรียก Instance:Destroy() หรือในฟังก์ชันที่เชื่อมต่อกับสัญญาณที่ส่งคืนโดย GetInstanceRemovedSignal()
พารามิเตอร์
ส่งค่ากลับ
GetInstanceAddedSignal
ให้แท็ก (สตริง) วิธีนี้จะส่งสัญญาณที่จะยิงภายใต้สองเงื่อนไข:
แท็กจะถูกกำหนดให้กับตัวอย่างภายใน DataModel โดยใช้ CollectionService:AddTag() หรือ Instance:AddTag()
ตัวอย่างที่มีแท็กที่กำหนดไว้ถูกเพิ่มเป็นลูกหลานของ DataModel ตัวอย่างเช่นโดยการตั้ง Instance.Parent หรือคล้ายกัน
การโทรต่อไปนี้กับวิธีนี้ด้วยแท็กเดียวกันจะส่งสัญญาณวัตถุเดียวกันกลับพิจารณาเรียก GetTagged() เพื่อรับรายการของตัวอย่างที่มีแท็กแล้ว (และดังนั้นจะไม่ส่งเหตุการณ์หากพวกเขาอยู่ใน DataModel แล้ว)
ดูเพิ่มเติม 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
ให้แท็ก (สตริง) วิธีนี้จะส่งสัญญาณที่จะยิงภายใต้สองเงื่อนไข:
แท็กจะถูกลบออกจากตัวอย่างภายใน DataModel โดยใช้ CollectionService:RemoveTag() หรือ Instance:RemoveTag()
ตัวอย่างที่มีแท็กที่กำหนดถูกลบออกเป็นลูกหลานของ DataModel ตัวอย่างเช่นโดยการปิดการตั้งค่า Instance.Parent หรือคล้ายกัน
การโทรต่อไปนี้กับวิธีนี้ด้วยแท็กเดียวกันจะส่งสัญญาณวัตถุเดียวกันกลับสัญญาณมีประโยชน์ในการล้างทรัพยากรที่ใช้โดยตัวอย่างที่เคยมีแท็ก เช่น การตัดการเชื่อมต่อ
ดูเพิ่มเติม 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
วิธีนี้จะคืนเป็นคอลเลกชันของตัวอย่างที่มีแท็กที่กำหนดซึ่งเป็นบุตรหลานของ DataModelการลบแท็กโดยใช้ CollectionService:RemoveTag() หรือ Instance:RemoveTag() จะช่วยให้วิธีนี้ไม่ส่งคืนพวกเขา
หากต้องการตรวจจับตัวอย่างทั้งหมดที่มีแท็ก, ทั้งสองแสดง และ อนาคต, ใช้วิธีนี้ในการเลื่อนผ่านตัวอย่างในขณะที่ยังทำการเชื่อมต่อกับสัญญาณที่ส่งกลับโดย GetInstanceAddedSignal()
วิธีนี้ไม่รับประกันการสั่งซื้อของตัวอย่างที่ส่งคืนใดๆนอกจากนี้ยังเป็นไปได้ที่ตัวอย่างจะมีแท็กที่กำหนดให้กับพวกเขา แต่ไม่ใช่ลูกหลานของ DataModel ตัวอย่างเช่นพ่อของพวกเขาคือ nil วิธีนี้จะไม่ส่งคืนตัวอย่างดังกล่าว
พารามิเตอร์
แท็กที่จะค้นหา
ส่งค่ากลับ
ชุดของตัวอย่างทั้งหมดที่มีแท็ก
ตัวอย่างโค้ด
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
เมื่อให้ Instance อินสแตนซ์
วิธีนี้มีประโยชน์เมื่อคุณต้องการทำอะไรบางอย่างด้วยแท็กติดตามหลายตัวในครั้งเดียว แต่ไม่มีประสิทธิภาพในการตรวจสอบการมีอยู่ของแท็กเดียวสำหรับสิ่งนี้ใช้ HasTag() เพื่อตรวจสอบแท็กเดียว
พารามิเตอร์
ตัวอย่างที่แท็กควรถูกส่งคืน
ส่งค่ากลับ
ชุดของสตริงที่เป็นแท็กที่ใช้กับตัวอย่างที่กำหนด
ตัวอย่างโค้ด
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
วิธีนี้จะคืนว่าให้แท็ก Instance ที่กำหนดมีหรือไม่
โดยขยาย แท็กใดๆ ที่ส่งคืนโดยการโทรไปที่ GetTags() บนตัวอย่างจะกลับมาเป็น true เมื่อใช้วิธีนี้
พารามิเตอร์
ตัวอย่างที่จะตรวจสอบการมีอยู่ของแท็ก
แท็กที่ตรวจสอบ
ส่งค่ากลับ
ว่าตัวอย่างมีแท็กหรือไม่
ตัวอย่างโค้ด
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
วิธีนี้ลบแท็กออกจากตัวอย่าง การลบแท็กสำเร็จจะเปิดสัญญาณที่สร้างขึ้นโดย GetInstanceRemovedSignal() ด้วยแท็กที่กำหนด การลบแท็กสำเร็จจะยิงสัญญาณที่สร้างขึ้นโดย ด้วยแท็กที่กำหนด
เมื่อลบป้ายชื่อ มันเป็นที่นิยมที่บางทรัพยากรจะถูกใช้เพื่อให้ป้ายชื่อมีฟังก์ชัน เช่น การเชื่อมต่อกิจกรรมหรือตารางเพื่อป้องกันการรั่วไหลของหน่วยความจํา เป็นความคิดที่ดีที่จะทําความสะอาดพวกเขา (ตัดการเชื่อมต่อ, ตั้งค่าเป็น nil เป็นต้น) เมื่อไม่จําเป็นต้องใช้แท็กอีกต่อไป
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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")