Collection Service
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Tóm tắt
Phương Pháp
CreateCollection(query: string,root: Instance):Collection |
GetCollection(class: string):Instances |
GetCollections(instance: Instance):{any} |
Sự Kiện
ItemAdded(instance: Instance):RBXScriptSignal |
ItemRemoved(instance: Instance):RBXScriptSignal |
TagAdded(tag: string):RBXScriptSignal |
TagRemoved(tag: string):RBXScriptSignal |
Tài Liệu Tham Khảo API
Phương Pháp
Add Tag
Create Collection
Lợi Nhuận
Get Collection
Get Collections
Get Instance Added Signal
Tham Số
Lợi Nhuận
Mẫu mã
Gạch Chết Sử Dụng 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
-- Lưu các kết nối để có thể ngắt kết nối khi thẻ bị xóa
local connections = {}
local function onInstanceAdded(object)
-- Xác nhận rằng đối tượng có thẻ này là một BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- Nếu có một kết nối đã lưu trên đối tượng này, ngắt kết nối/xóa nó
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Lắng nghe thẻ này được áp dụng cho các đối tượng
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Cũng phát hiện bất kỳ đối tượng nào đã có thẻ
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
endGet Instance Removed Signal
Tham Số
Lợi Nhuận
Mẫu mã
Gạch Chết Sử Dụng 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
-- Lưu các kết nối để có thể ngắt kết nối khi thẻ bị xóa
local connections = {}
local function onInstanceAdded(object)
-- Xác nhận rằng đối tượng có thẻ này là một BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- Nếu có một kết nối đã lưu trên đối tượng này, ngắt kết nối/xóa nó
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Lắng nghe thẻ này được áp dụng cho các đối tượng
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Cũng phát hiện bất kỳ đối tượng nào đã có thẻ
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
endGet Tagged
Tham Số
Lợi Nhuận
Instances
Mẫu mã
Gạch Chết Sử Dụng 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
-- Lưu các kết nối để có thể ngắt kết nối khi thẻ bị xóa
local connections = {}
local function onInstanceAdded(object)
-- Xác nhận rằng đối tượng có thẻ này là một BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- Nếu có một kết nối đã lưu trên đối tượng này, ngắt kết nối/xóa nó
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Lắng nghe thẻ này được áp dụng cho các đối tượng
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Cũng phát hiện bất kỳ đối tượng nào đã có thẻ
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
endGet Tags
Tham Số
Lợi Nhuận
Mẫu mã
Sử dụng Thẻ và CollectionService
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Thêm một thẻ
CollectionService:AddTag(object, "Deadly")
-- Truy vấn một thẻ
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " là chết người")
end
-- Liệt kê thẻ trên một đối tượng
local tags = CollectionService:GetTags(object)
print("Đối tượng " .. object:GetFullName() .. " có các thẻ: " .. table.concat(tags, ", "))
-- Xóa một thẻ
CollectionService:RemoveTag(object, "Deadly")Has Tag
Lợi Nhuận
Mẫu mã
Sử dụng Thẻ và CollectionService
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Thêm một thẻ
CollectionService:AddTag(object, "Deadly")
-- Truy vấn một thẻ
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " là chết người")
end
-- Liệt kê thẻ trên một đối tượng
local tags = CollectionService:GetTags(object)
print("Đối tượng " .. object:GetFullName() .. " có các thẻ: " .. table.concat(tags, ", "))
-- Xóa một thẻ
CollectionService:RemoveTag(object, "Deadly")Remove Tag
Lợi Nhuận
()
Mẫu mã
Sử dụng Thẻ và CollectionService
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Thêm một thẻ
CollectionService:AddTag(object, "Deadly")
-- Truy vấn một thẻ
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " là chết người")
end
-- Liệt kê thẻ trên một đối tượng
local tags = CollectionService:GetTags(object)
print("Đối tượng " .. object:GetFullName() .. " có các thẻ: " .. table.concat(tags, ", "))
-- Xóa một thẻ
CollectionService:RemoveTag(object, "Deadly")Sự Kiện
Item Added
Item Removed