概要
方法
GetCollection(class: string):Instances |
活動
ItemAdded(instance: Instance):RBXScriptSignal |
ItemRemoved(instance: Instance):RBXScriptSignal |
TagAdded(tag: string):RBXScriptSignal |
TagRemoved(tag: string):RBXScriptSignal |
API 參考
方法
GetCollection
GetInstanceAddedSignal
參數
範例程式碼
致命磚塊使用 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
-- 儲存 連接,以便在標籤被移除時可以斷開
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
參數
範例程式碼
致命磚塊使用 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
-- 儲存 連接,以便在標籤被移除時可以斷開
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
範例程式碼
致命磚塊使用 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
-- 儲存 連接,以便在標籤被移除時可以斷開
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