CollectionService
*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.
CollectionService gestisce gruppi (collezioni) di istanze con tag .Le etichette sono set di stringhe applicate alle istanze che si replicano dal server al client.Sono anche serializzati quando vengono salvati i luoghi.
L'uso principale di CollectionService è quello di registrare istanze con tag specifici che puoi utilizzare per estendere il loro comportamento.Se ti trovi ad aggiungere lo stesso script a molte istanze diverse, uno script che utilizza CollectionService potrebbe essere migliore.
Le etichette possono essere aggiunte o rimosse attraverso i metodi di questa classe come AddTag() o RemoveTag() .Possono anche essere gestiti direttamente in Studio attraverso la sezione Tag delle proprietà di un'esempioo attraverso lo strumento Editor di tag incorporato.
Replicazione
Quando le etichette si replicano, tutte le etichette su un'istanza si replicano allo stesso tempo .Pertanto, se imposti un tag su un'istanza dal client, quindi aggiungi/rimuovi un tag diverso sullo stesso istanza dal Server, le etichette locali sull'istanza del client vengono sovrascritte.In StreamingEnabled luoghi, le istanze possono essere scaricate mentre lasciano l'area streaming del client.Se un'istanza del genere rientra nell'area streaming, le proprietà e i tag verranno ri-sincronizzati dal Server.Questo può causare cambiamenti apportati da LocalScripts a essere sovrascritti/rimossi.
Sommario
Proprietà
Metodi
Applica un tag a un Instance .
Restituisce un array di tutti i tag nell'esperienza.
Restituisce un segnale che si attiva quando un tag dato viene aggiunto a un'esempio.
Restituisce un segnale che si attiva quando un tag dato viene rimosso da un'esempio.
Restituisce un array di istanze nel gioco con un tag dato.
Ottiene un array di tutti i tag applicati a una determinata esempio.
Verifica se un'istanza abbia un tag dato.
Rimuove un tag da un'esempio.
Eventi
Si attiva quando viene aggiunto un tag a un'istanza e il tag aggiunto è l'unica occorrenza di quel tag nel Posto.
Si accende quando un tag viene rimosso da un'istanza e il tag rimosso non viene più utilizzato da nessuna parte nel Posto.
Proprietà
Metodi
AddTag
Parametri
Restituzioni
GetInstanceAddedSignal
Parametri
Restituzioni
Campioni di codice
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
Parametri
Restituzioni
Campioni di codice
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
Parametri
Restituzioni
Campioni di codice
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
Parametri
Restituzioni
Campioni di codice
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
Parametri
Restituzioni
Campioni di codice
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
Parametri
Restituzioni
Campioni di codice
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")