CollectionService
*Bu içerik, yapay zekâ (beta) kullanılarak çevrildi ve hatalar içerebilir. Sayfayı İngilizce görüntülemek için buraya tıkla.
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
Özellikler
Yöntemler
Bir etiketi Instance 'ye uygular.
Deneyimdeki tüm etiketlerin bir diziini döndürür.
Verilen bir etiket bir durumeklendiğinde ateşleyen bir sinyal döndürür.
Belirli bir etiket bir durumkaldırıldığında ateşleyen bir sinyali döndürür.
Verilen bir etiketle oyundaki bir dizi instans döndürür.
Verilen bir durumuygulanan tüm etiketlerin bir dizi alınır.
Bir instansın belirli bir etikete sahip olup olmadığını kontrol edin.
Bir durumbir etiketi kaldırır.
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
GetInstanceAddedSignal
Parametreler
Dönüşler
Kod Örnekleri
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
Dönüşler
Kod Örnekleri
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
Parametreler
Dönüşler
Kod Örnekleri
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
Parametreler
Dönüşler
Kod Örnekleri
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
Parametreler
Dönüşler
Kod Örnekleri
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
Dönüşler
Kod Örnekleri
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")