CollectionService

Mostrar obsoleto
No creable
Servicio

CollectionService manages groups (collections) of instances with tags. Tags are sets of strings applied to objects that replicate from the server to the client. They are also serialized when places are saved.

The primary use of CollectionService is to register instances with specific tags that you can use to extend their behavior. If you find yourself adding the same script to many different objects, perhaps a script that uses CollectionService would be better.

Tags can be added or removed through this class' methods such as AddTag() or RemoveTag(). They can also be managed directly in Studio through the Tags section of an instance's properties, or through the built‑in Tag Editor tool.

Replication

When tags replicate, all tags on an object replicate at the same time. Therefore, if you set a tag on an object from the client then add/remove a different tag on the same object from the server, the client's local tags on the object are overwritten. In StreamingEnabled places, instances can be unloaded as they leave the client's streamed area. If such an instance re-enters the streamed area, properties and tags will be re-synchronized from the server. This can cause changes made by LocalScripts to be overwritten/removed.

Resumen

Métodos

Eventos

Propiedades

Métodos

AddTag

void

AddTag will apply a tag to an Instance, doing nothing if the tag is already applied to the instance. Successfully adding a tag will fire a signal created by CollectionService:GetInstanceAddedSignal() with the given tag.

Warning: When tagging an instance, it is common that some resources are used to give the tag its functionality, e.g. event connections or tables. To prevent memory leaks, it is a good idea to clean these up (disconnect, set to nil, etc) when no longer needed for a tag. Do this when calling CollectionService:RemoveTag(), calling Instance:Destroy() or in a function connected to a signal returned by CollectionService:GetInstanceRemovedSignal().

Parámetros

instance: Instance
tag: string

Devuelve

void

GetAllTags

Escribir paralelo

Devuelve

GetInstanceAddedSignal

GetInstanceAdded is given a tag (a string) and returns a signal which fires under two conditions:

Subsequent calls to this method with the same tag return the same signal object. Consider also calling CollectionService:GetTagged() to get a list of objects that already have a tag (and thus won't fire the event if they already are in the DataModel).

See also CollectionService:GetInstanceRemovedSignal(), which returns an event that fires under similar conditions.

Parámetros

tag: string

The tag to watch for.


Devuelve

An event that fires when you add the tag to an instance.

Muestras de código

Deadly Bricks using CollectionService

local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if human then
human.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
-- This table maps BaseParts with the tag to their Touched connections
local connections = {}
local function onInstanceAdded(object)
-- Remember that any tag can be applied to any object, so there's no
-- guarantee 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 we made a connection on this object, disconnect it (prevent memory leaks)
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

GetInstanceRemoved is given a tag (a string) and returns a signal which fires under two conditions:

Subsequent calls to this method with the same tag return the same signal object. The signal is useful for cleaning up resources used by objects that once had tags, such as disconnecting connections.

See also CollectionService:GetInstanceAddedSignal(), which returns an event that fires under similar conditions.

Parámetros

tag: string

The tag to watch for.


Devuelve

An event that fires when you remove the tag from an instance.

Muestras de código

Deadly Bricks using CollectionService

local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if human then
human.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
-- This table maps BaseParts with the tag to their Touched connections
local connections = {}
local function onInstanceAdded(object)
-- Remember that any tag can be applied to any object, so there's no
-- guarantee 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 we made a connection on this object, disconnect it (prevent memory leaks)
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

Escribir paralelo

GetTagged returns an array of objects with a given tag which are descendants of the DataModel (game). Such tags have been added using CollectionService:AddTag(), and removing a tag using CollectionService:RemoveTag() will ensure this method does not return them. Although the name of this method is past-tense, this method only returns objects presently tagged with the given tag. It will not return objects that once had a tag but no longer have it.

If you want to detect all objects with a tag, both present and future, use this method to iterate over objects while also making a connection to a signal returned by CollectionService.GetInstanceAddedSignal.

This method does not guarantee any ordering of the returned objects. Additionally, it is possible that objects can have the given tag assigned to them, but not be a descendant of the DataModel, i.e. its parent is nil. This method will not return such objects.

Parámetros

tag: string

The tag to search for.


Devuelve

An array of all instances with the tag.

Muestras de código

Deadly Bricks using CollectionService

local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if human then
human.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
-- This table maps BaseParts with the tag to their Touched connections
local connections = {}
local function onInstanceAdded(object)
-- Remember that any tag can be applied to any object, so there's no
-- guarantee 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 we made a connection on this object, disconnect it (prevent memory leaks)
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

Escribir paralelo

GetTags is given an instance and returns an array of strings, which are the tags applied to the given object.


local CollectionService = game:GetService("CollectionService")
local object = workspace.Model
local tags = CollectionService:GetTags(object)
print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))

This method is useful when you want to do something with multiple tags at once on an object. However, it would be inefficient to use this method to check for the existence of a single tag. For this, use CollectionService:HasTag() to check for a single tag.

Parámetros

instance: Instance

The object whose tags should be returned.


Devuelve

An array of strings which are the tags applied to the given object.

Muestras de código

Using Tags and CollectionService

local CollectionService = game:GetService("CollectionService")
local object = script.Parent.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

Escribir paralelo

HasTag returns whether a given object has a tag.

By extension, any tags returned by a call to CollectionService:GetTags() on an object will return true when used with this method.

Parámetros

instance: Instance

The instance to check for the presence of a tag.

tag: string

The tag to check for.


Devuelve

Whether the instance has the tag.

Muestras de código

Using Tags and CollectionService

local CollectionService = game:GetService("CollectionService")
local object = script.Parent.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

void

RemoveTag will remove a tag from an instance. This method will not throw an error if the object did not have the tag in the first place. Successfully removing a tag will fire a signal created by CollectionService:GetInstanceRemovedSignal() with the given tag.

When removing a tag, it is common that some resources are used to give the tag its functionality, e.g. event connections or tables. To prevent memory leaks, it is a good idea to clean these up (disconnect, set to nil, etc) when no longer needed for a tag.

Parámetros

instance: Instance

The instance to remove the tag from.

tag: string

The tag to remove from the instance.


Devuelve

void

Muestras de código

Using Tags and CollectionService

local CollectionService = game:GetService("CollectionService")
local object = script.Parent.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")

Eventos

TagAdded

This event fires when a tag is added to an object and the added tag is the only occurrence of that tag in the place.

Parámetros

tag: string

TagRemoved

This event fires when a tag is removed from an object and the removed tag is no longer used anywhere in the place.

Parámetros

tag: string