CollectionService

Artık kullanılmayanları göster

*Bu içerik, yapay zekâ (beta) kullanılarak çevrildi ve hatalar içerebilir. Sayfayı İngilizce görüntülemek için buraya tıkla.

Oluşturulamaz
Hizmet

CollectionService grupları yönetir (koleksiyonlar) durumların etiketleri ile.Etiketler, sunucudan müşteriye yeniden yansıyan örneklere uygulanan dize setleridir.Yerler kaydedildiğinde de serilize edilirler.

CollectionService 'nin birincil kullanımı, davranışlarını genişletmek için kullanabileceğiniz belirli etiketlerle instansları kaydetmektir.Aynı senaryoyu birçok farklı duruma eklediğinizi görürseniz, CollectionService kullanan bir senaryo daha iyi olabilir.

Etiketler bu sınıfın yöntemleri aracılığıyla AddTag() veya RemoveTag() gibi eklenebilir veya kaldırılabilir.Ayrıca, bir durumözelliklerinin Etiketler bölümünde doğrudan yönetilebilir veya dahili Etiket Düzenleyici aracı aracılığıyla yönetilebilir.

Replikasyon

Etiketler yeniden yapıldığında, bir örnekteki tüm etiketler aynı anda yeniden yapılır .Bu nedenle, istemciden bir örneğe bir etiket ayarlarsanız, aynı sunucudan aynı örnek üzerinde farklı bir etiket ekleyin/kaldırın, istemcinin yerel etiketleri üzerinde aynı örnek üzerinde yeniden yazılır.StreamingEnabled yerlerde, örnekler müşterinin yayınlanan alanını terk ettiğinde yükleri kaldırılabilir.Böyle bir örnek yayınlanan alana yeniden girdiğinde, özellikler ve etiketler sunucudan yeniden senkronize edilecektir.Bu, LocalScripts tarafından yapılan değişikliklerin üzerine yazılmasına/kaldırılmasına neden olabilir.

Özet

Yöntemler

Olaylar

  • Bir etiket bir örneğe eklendiğinde ve eklenen etiket o etiketin dünyatek olay olduğunda ateş eder.

  • Bir etiket bir örnekten kaldırıldığında ve kaldırılan etiket artık herhangi bir dünyakullanılmadığında ateş eder.

Özellikler

Yöntemler

AddTag

()

Parametreler

instance: Instance
Varsayılan değer: ""
tag: string
Varsayılan değer: ""

Dönüşler

()

GetAllTags

Paralel yaz

Dönüşler

GetInstanceAddedSignal

Parametreler

tag: string
Varsayılan değer: ""

Dönüşler

Kod Örnekleri

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

Parametreler

tag: string
Varsayılan değer: ""

Dönüşler

Kod Örnekleri

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
Paralel yaz

Parametreler

tag: string
Varsayılan değer: ""

Dönüşler

Instances

Kod Örnekleri

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

Paralel yaz

Parametreler

instance: Instance
Varsayılan değer: ""

Dönüşler

Kod Örnekleri

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

Paralel yaz

Parametreler

instance: Instance
Varsayılan değer: ""
tag: string
Varsayılan değer: ""

Dönüşler

Kod Örnekleri

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

()

Parametreler

instance: Instance
Varsayılan değer: ""
tag: string
Varsayılan değer: ""

Dönüşler

()

Kod Örnekleri

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")

Olaylar

TagAdded

Parametreler

tag: string

TagRemoved

Parametreler

tag: string