学习
引擎类
CollectionService
无法创建
服务

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


概要
继承成员

API 参考
方法
AddTag
功能: Basic
CollectionService:AddTag(
instance:Instance, tag:string
):()
参数
instance:Instance
tag:string
返回
()

GetAllTags
写入并联
功能: Basic
CollectionService:GetAllTags():{any}
返回

GetCollection
已弃用

GetInstanceAddedSignal
功能: Basic
CollectionService:GetInstanceAddedSignal(tag:string):RBXScriptSignal
参数
tag:string
代码示例
死亡砖块使用集合服务
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
-- 保存连接,以便在移除标签时可以断开
local connections = {}
local function onInstanceAdded(object)
-- 确认带有此标签的对象是 BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- 如果此对象上有存储的连接,则断开/移除它
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- 监听此标签应用于对象
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- 还要检测任何已经带有标签的对象
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end

GetInstanceRemovedSignal
功能: Basic
CollectionService:GetInstanceRemovedSignal(tag:string):RBXScriptSignal
参数
tag:string
代码示例
死亡砖块使用集合服务
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
-- 保存连接,以便在移除标签时可以断开
local connections = {}
local function onInstanceAdded(object)
-- 确认带有此标签的对象是 BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- 如果此对象上有存储的连接,则断开/移除它
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- 监听此标签应用于对象
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- 还要检测任何已经带有标签的对象
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end

GetTagged
写入并联
功能: Basic
CollectionService:GetTagged(tag:string):Instances
参数
tag:string
返回
Instances
代码示例
死亡砖块使用集合服务
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
-- 保存连接,以便在移除标签时可以断开
local connections = {}
local function onInstanceAdded(object)
-- 确认带有此标签的对象是 BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- 如果此对象上有存储的连接,则断开/移除它
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- 监听此标签应用于对象
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- 还要检测任何已经带有标签的对象
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end

GetTags
写入并联
功能: Basic
CollectionService:GetTags(instance:Instance):{any}
参数
instance:Instance
返回
代码示例
使用标签和 CollectionService
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- 添加一个标签
CollectionService:AddTag(object, "Deadly")
-- 查询一个标签
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " 是致命的")
end
-- 列出对象上的标签
local tags = CollectionService:GetTags(object)
print("对象 " .. object:GetFullName() .. " 有标签: " .. table.concat(tags, ", "))
-- 移除一个标签
CollectionService:RemoveTag(object, "Deadly")

HasTag
写入并联
功能: Basic
CollectionService:HasTag(
instance:Instance, tag:string
参数
instance:Instance
tag:string
返回
代码示例
使用标签和 CollectionService
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- 添加一个标签
CollectionService:AddTag(object, "Deadly")
-- 查询一个标签
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " 是致命的")
end
-- 列出对象上的标签
local tags = CollectionService:GetTags(object)
print("对象 " .. object:GetFullName() .. " 有标签: " .. table.concat(tags, ", "))
-- 移除一个标签
CollectionService:RemoveTag(object, "Deadly")

RemoveTag
功能: Basic
CollectionService:RemoveTag(
instance:Instance, tag:string
):()
参数
instance:Instance
tag:string
返回
()
代码示例
使用标签和 CollectionService
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- 添加一个标签
CollectionService:AddTag(object, "Deadly")
-- 查询一个标签
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " 是致命的")
end
-- 列出对象上的标签
local tags = CollectionService:GetTags(object)
print("对象 " .. object:GetFullName() .. " 有标签: " .. table.concat(tags, ", "))
-- 移除一个标签
CollectionService:RemoveTag(object, "Deadly")

活动
ItemAdded
已弃用

ItemRemoved
已弃用

TagAdded
功能: Basic
CollectionService.TagAdded(tag:string):RBXScriptSignal
参数
tag:string

TagRemoved
功能: Basic
CollectionService.TagRemoved(tag:string):RBXScriptSignal
参数
tag:string

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。