概要
方法
GetCollection(class: string):Instances |
活动
ItemAdded(instance: Instance):RBXScriptSignal |
ItemRemoved(instance: Instance):RBXScriptSignal |
TagAdded(tag: string):RBXScriptSignal |
TagRemoved(tag: string):RBXScriptSignal |
API 参考
方法
GetCollection
GetInstanceAddedSignal
参数
代码示例
死亡砖块使用集合服务
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)
endGetInstanceRemovedSignal
参数
代码示例
死亡砖块使用集合服务
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)
endGetTagged
参数
返回
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)
endGetTags
参数
返回
代码示例
使用标签和 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
返回
代码示例
使用标签和 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
返回
()
代码示例
使用标签和 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