CollectionService

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务

CollectionService 管理具有 标签 的实例群组(收集)。标签是用于从服务器复制到客户端的实例的一组字符串。当保存地点时,它们也会序列化。

CollectionService 的主要用途是注册具有特定标签的实例,您可以使用它们来扩展其行为。如果你发现自己在很多不同的实例上添加了相同的脚本,使用 CollectionService 的脚本可能会更好。

标签可以通过此类方法添加或移除,例如 AddTag()RemoveTag() 。它们还可以直接在 Studio 中管理通过实例属性的 标签 部分,或通过内置的 标签编辑器 工具。

复制

当标签复制时, 所有标签在实例上同时复制 。因此,如果您从客户端设置标签在实例上,然后从服务器上添加/删除不同的标签 不同 标签在同一实例上,客户端的本地标签在实例上将被覆盖。在 StreamingEnabled 地点,实例可以在离开客户端的流区时卸载。如果这样的实例重新进入传输区域,属性和标签将从服务器重新同步。这可能导致由 LocalScripts 所做的更改被覆盖/删除。

概要

方法

活动

属性

方法

AddTag

()

该方法对 Instance 应用标签,如果标签已应用到该实例,则不做任何操作。成功添加标签会触发由 GetInstanceAddedSignal() 创建的信号,其标签与指定的标签相同。

警告
  • 服务器稍后添加或移除了该实例上的标签,客户端添加的标签将被丢弃,因为服务器复制所有标签并覆盖以前的标签。

  • 当标记实例时,常见的是一些资源被用来给标签提供功能,例如事件连接或表。为了防止内存泄漏,清理这些(断开、设置为 nil 等)当标签不再需要时是个好主意。在调用 RemoveTag() 、调用 Instance:Destroy() 或在与 GetInstanceRemovedSignal() 返回的信号连接的函数中执行时,执行此操作。

参数

instance: Instance
默认值:""
tag: string
默认值:""

返回

()

GetAllTags

写入并联

返回体验中的所有标签的阵列。


返回

GetInstanceAddedSignal

给予标签(字符串),此方法返回一个信号,其在两个条件下发射:

使用相同标签对此方法进行后续调用返回相同的信号对象。还需要考虑调用 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

给予标签(字符串),此方法返回一个信号,其在两个条件下发射:

使用相同标签对此方法进行后续调用返回相同的信号对象。信号对于清理曾经拥有标签的实例使用的资源有用,例如断开连接。

还看到 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() 移除标签可确保此方法不会返回它们。

如果您想检测标签的所有实例, both present 未来,使用此方法循环过实例,同时还与由 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