CollectionService

Afficher les obsolètes

*Ce contenu est traduit en utilisant l'IA (Beta) et peut contenir des erreurs. Pour consulter cette page en anglais, clique ici.

Création impossible
Service

CollectionService gère des groupes ( collections ) d'instances avec des balises .Les balises sont des ensembles de chaînes appliquées aux instances qui se répliquent du serveur au client.Ils sont également sérialisés lorsque les lieux sont sauvegardés.

L'utilisation principale de CollectionService est d'enregistrer des instances avec des balises spécifiques que vous pouvez utiliser pour étendre leur comportement.Si vous vous retrouvez à ajouter le même script à de nombreuses instances différentes, un script qui utilise CollectionService peut être meilleur.

Les balises peuvent être ajoutées ou supprimées via les méthodes de cette classe telles que AddTag() ou RemoveTag() .Ils peuvent également être gérés directement dans Studio via la section balises des propriétés d'une instance, ou via l'outil éditeur de balises intégré.

Réplication

Lorsque les balises se répliquent, toutes les balises sur une instance se répliquent en même temps .Si vous définissez une balise sur une instance à partir du client, puis ajoutez/supprimez une balise différente sur la même instance du serveur, les balises locales de l'instance du client sont remplacées.Dans StreamingEnabled endroits, les instances peuvent être déchargées lorsqu'elles quittent la zone diffusée du client.Si une telle instance réentre dans la zone diffusée, les propriétés et les balises seront ré synchronisées depuis le serveur.Cela peut provoquer des modifications apportées par LocalScripts d'être écrasées/supprimées.

Résumé

Méthodes

Événements

  • Se déclenche lorsqu'une balise est ajoutée à une instance et que la balise ajoutée est la seule occurrence de cette balise dans l'emplacement.

  • Se déclenche lorsqu'une balise est supprimée d'une instance et que la balise supprimée n'est plus utilisée nulle emplacementailleurs.

Propriétés

Méthodes

AddTag

()

Paramètres

instance: Instance
Valeur par défaut : ""
tag: string
Valeur par défaut : ""

Retours

()

GetAllTags

Écrire en parallèle

Retours

GetInstanceAddedSignal

Paramètres

tag: string
Valeur par défaut : ""

Retours

Échantillons de code

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

Paramètres

tag: string
Valeur par défaut : ""

Retours

Échantillons de code

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
Écrire en parallèle

Paramètres

tag: string
Valeur par défaut : ""

Retours

Instances

Échantillons de code

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

Écrire en parallèle

Paramètres

instance: Instance
Valeur par défaut : ""

Retours

Échantillons de code

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

Écrire en parallèle

Paramètres

instance: Instance
Valeur par défaut : ""
tag: string
Valeur par défaut : ""

Retours

Échantillons de code

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

()

Paramètres

instance: Instance
Valeur par défaut : ""
tag: string
Valeur par défaut : ""

Retours

()

Échantillons de code

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

Événements

TagAdded

Paramètres

tag: string

TagRemoved

Paramètres

tag: string