CollectionService

แสดงที่เลิกใช้งานแล้ว

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

ไม่สามารถสร้าง
บริการ

CollectionService จัดการกลุ่ม (คอลเลกชัน) ของตัวอย่างที่มีแท็ก **** แท็กคือชุดของสตริงที่ใช้กับตัวอย่างที่สําเร็จจากเซิร์ฟเวอร์ไปยังไคลเอนต์พวกเขายังถูกเซริซิสเมื่อสถานที่ถูกบันทึก

การใช้งานหลักของ CollectionService คือการลงทะเบียนตัวอย่างที่มีแท็กเฉพาะที่คุณสามารถใช้เพื่อขยายพฤติกรรมได้หากคุณพบว่าตัวเองเพิ่มสคริปต์เดียวกันไปยังหลายตัวอย่างที่แตกต่างกัน สคริปต์ที่ใช้ CollectionService อาจจะดีกว่า

แท็กสามารถเพิ่มหรือลบผ่านวิธีการของคลาสนี้เช่น AddTag() หรือ RemoveTag()พวกเขายังสามารถจัดการได้โดยตรงใน Studio ผ่านส่วน แท็ก อินสแตนซ์เอเดอร์แท็กที่สร้างขึ้น

การเลียนแบบ

เมื่อแท็กซ้ำ แท็กทั้งหมดในตัวอย่างจะซ้ำในเวลาเดียวกัน ดังนั้นหากคุณตั้งแท็กบนตัวอย่างจากไคลเอนต์แล้วเพิ่ม/ลบแท็ก ที่แตกต่าง บนตัวอย่างเดียวกันจากเซิร์ฟเวอร์แท็กท้องถิ่นบนตัวอย่างจะถูกเขียนทับใน StreamingEnabled สถานที่ ตัวอย่างสามารถถอดออกได้เมื่อพวกเขาออกจากพื้นที่สตรีมของไคลเอนต์หากตัวอย่างดังกล่าวเข้าสู่พื้นที่สตรีมอีกครั้ง คุณสมบัติและแท็กจะถูกซิงโครไลซ์ใหม่จากเซิร์ฟเวอร์สิ่งนี้อาจทำให้การเปลี่ยนแปลงที่ทำโดย LocalScripts ถูกเขียนทับ/ลบ

สรุป

วิธีการ

  • AddTag(instance : Instance,tag : string):()

    ใช้แท็กกับ Instance ที่

  • เขียนพร้อมๆ กัน

    คืนค่าเป็นคอลเลกชันของแท็กทั้งหมดในประสบการณ์

  • ส่งสัญญาณที่จะยิงเมื่อแท็กที่กำหนดถูกเพิ่มลงในตัวอย่าง

  • ส่งสัญญาณที่จะยิงเมื่อแท็กที่กำหนดถูกลบออกจากตัวอย่าง

  • GetTagged(tag : string):Instances
    เขียนพร้อมๆ กัน

    คืนค่าเป็นคอลเลกชันของตัวอย่างในเกมด้วยแท็กที่กำหนดไว้

  • GetTags(instance : Instance):Array
    เขียนพร้อมๆ กัน

    รับคอลเลกชันของแท็กทั้งหมดที่ใช้กับตัวอย่างที่กำหนด

  • HasTag(instance : Instance,tag : string):boolean
    เขียนพร้อมๆ กัน

    ตรวจสอบว่าตัวอย่างมีแท็กที่กำหนดหรือไม่

  • RemoveTag(instance : Instance,tag : string):()

    ลบแท็กจากตัวอย่าง

อีเวนต์

  • ไฟเมื่อแท็กถูกเพิ่มลงในตัวอย่างและแท็กที่เพิ่มมาเป็นการปรากฏเดียวของแท็กนั้นในสถานที่

  • จะเกิดไฟไหม้เมื่อแท็กถูกลบออกจากตัวอย่างและแท็กที่ถูกลบไม่ถูกใช้ที่ใดในสถานที่อีกต่อไป

คุณสมบัติ

วิธีการ

AddTag

()

วิธีนี้ใช้แท็กกับ Instance โดยไม่ทำอะไรถ้าแท็กถูกใช้กับตัวอย่างนั้นแล้วการเพิ่มแท็กสําเร็จจะส่งสัญญาณที่สร้างขึ้นโดย GetInstanceAddedSignal() ด้วยแท็กที่กำหนดไว้

คําเตือน
  • แท็กของตัวอย่างที่ถูกเพิ่มบนเครื่องคอมพิวเตอร์จะถูกลบหากเซิร์ฟเวอร์เพิ่มหรือลบแท็กบนตัวอย่างนั้นในภายหลังเพราะเซิร์ฟเวอร์สำเนาทั้งหมดแท็กและเขียนทับแท็กก่อนหน้า

  • อินสแตนซ์มันเป็นเรื่องธรรมดาที่ทรัพยากรบางอย่างจะถูกใช้เพื่อให้ป้ายกำกับมีฟังก์ชัน เช่น การเชื่อมต่อกิจกรรมหรือตารางเพื่อป้องกันการรั่วไหลของหน่วยความจํา เป็นความคิดที่ดีที่จะทําความสะอาดพวกเขา (ตัดการเชื่อมต่อ, ตั้งค่าเป็น nil เป็นต้น) เมื่อไม่จําเป็นต้องใช้แท็กอีกต่อไปทำเช่นนี้เมื่อเรียก RemoveTag() , เรียก Instance:Destroy() หรือในฟังก์ชันที่เชื่อมต่อกับสัญญาณที่ส่งคืนโดย GetInstanceRemovedSignal()

พารามิเตอร์

instance: Instance
ค่าเริ่มต้น: ""
tag: string
ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

GetAllTags

เขียนพร้อมๆ กัน

คืนค่าเป็นคอลเลกชันของแท็กทั้งหมดในประสบการณ์


ส่งค่ากลับ

GetInstanceAddedSignal

ให้แท็ก (สตริง) วิธีนี้จะส่งสัญญาณที่จะยิงภายใต้สองเงื่อนไข:

  • แท็กจะถูกกำหนดให้กับตัวอย่างภายใน DataModel โดยใช้ CollectionService:AddTag() หรือ Instance:AddTag()

  • ตัวอย่างที่มีแท็กที่กำหนดไว้ถูกเพิ่มเป็นลูกหลานของ DataModel ตัวอย่างเช่นโดยการตั้ง Instance.Parent หรือคล้ายกัน

การโทรต่อไปนี้กับวิธีนี้ด้วยแท็กเดียวกันจะส่งสัญญาณวัตถุเดียวกันกลับพิจารณาเรียก GetTagged() เพื่อรับรายการของตัวอย่างที่มีแท็กแล้ว (และดังนั้นจะไม่ส่งเหตุการณ์หากพวกเขาอยู่ใน DataModel แล้ว)

ดูเพิ่มเติม GetInstanceRemovedSignal() ซึ่งจะคืนเหตุการณ์ที่จะเกิดขึ้นภายใต้เงื่อนไขที่คล้ายกัน

พารามิเตอร์

tag: string

แท็กที่ต้องระวัง

ค่าเริ่มต้น: ""

ส่งค่ากลับ

อีเวนต์ที่จะยิงเมื่อคุณเพิ่มแท็กให้กับตัวอย่าง

ตัวอย่างโค้ด

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.

Deadly Bricks using CollectionService

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() ซึ่งจะคืนเหตุการณ์ที่จะเกิดขึ้นภายใต้เงื่อนไขที่คล้ายกัน

พารามิเตอร์

tag: string

แท็กที่ต้องระวัง

ค่าเริ่มต้น: ""

ส่งค่ากลับ

อีเวนต์ที่จะยิงเมื่อคุณลบแท็กออกจากตัวอย่าง

ตัวอย่างโค้ด

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.

Deadly Bricks using CollectionService

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

Instances
เขียนพร้อมๆ กัน

วิธีนี้จะคืนเป็นคอลเลกชันของตัวอย่างที่มีแท็กที่กำหนดซึ่งเป็นบุตรหลานของ DataModelการลบแท็กโดยใช้ CollectionService:RemoveTag() หรือ Instance:RemoveTag() จะช่วยให้วิธีนี้ไม่ส่งคืนพวกเขา

หากต้องการตรวจจับตัวอย่างทั้งหมดที่มีแท็ก, ทั้งสองแสดง และ อนาคต, ใช้วิธีนี้ในการเลื่อนผ่านตัวอย่างในขณะที่ยังทำการเชื่อมต่อกับสัญญาณที่ส่งกลับโดย GetInstanceAddedSignal()

วิธีนี้ไม่รับประกันการสั่งซื้อของตัวอย่างที่ส่งคืนใดๆนอกจากนี้ยังเป็นไปได้ที่ตัวอย่างจะมีแท็กที่กำหนดให้กับพวกเขา แต่ไม่ใช่ลูกหลานของ DataModel ตัวอย่างเช่นพ่อของพวกเขาคือ nil วิธีนี้จะไม่ส่งคืนตัวอย่างดังกล่าว

พารามิเตอร์

tag: string

แท็กที่จะค้นหา

ค่าเริ่มต้น: ""

ส่งค่ากลับ

Instances

ชุดของตัวอย่างทั้งหมดที่มีแท็ก

ตัวอย่างโค้ด

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.

Deadly Bricks using CollectionService

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() เพื่อตรวจสอบแท็กเดียว

พารามิเตอร์

instance: Instance

ตัวอย่างที่แท็กควรถูกส่งคืน

ค่าเริ่มต้น: ""

ส่งค่ากลับ

ชุดของสตริงที่เป็นแท็กที่ใช้กับตัวอย่างที่กำหนด

ตัวอย่างโค้ด

This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.

Using Tags and 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 เมื่อใช้วิธีนี้

พารามิเตอร์

instance: Instance

ตัวอย่างที่จะตรวจสอบการมีอยู่ของแท็ก

ค่าเริ่มต้น: ""
tag: string

แท็กที่ตรวจสอบ

ค่าเริ่มต้น: ""

ส่งค่ากลับ

ว่าตัวอย่างมีแท็กหรือไม่

ตัวอย่างโค้ด

This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.

Using Tags and 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 เป็นต้น) เมื่อไม่จําเป็นต้องใช้แท็กอีกต่อไป

พารามิเตอร์

instance: Instance

ตัวอย่างที่จะลบแท็กออก

ค่าเริ่มต้น: ""
tag: string

อินสแตนซ์

ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.

Using Tags and 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")

อีเวนต์

TagAdded

อีเวนต์นี้จะเกิดขึ้นเมื่อแท็กถูกเพิ่มลงในตัวอย่างและแท็กที่เพิ่มมาเป็นแท็กเดียวเท่านั้นในสถานที่นั้น

พารามิเตอร์

tag: string

TagRemoved

อีเวนต์นี้จะเกิดขึ้นเมื่อแท็กถูกลบออกจากตัวอย่างและแท็กที่ถูกลบไม่ถูกใช้ที่ใดในสถานที่อีกต่อไป

พารามิเตอร์

tag: string