If a object is tagged with CollectionService and the object destroys the tag get deleted?

hi, I was making a MiniGame script and i have a question if the tagged object gets deleted the tag will be deleted?

The tag is deleted from the instance once it is destroyed since it doesnt exist anymore. But if other instances have the same tag, they will not be affected at all.

No, the tag doens’t get’s deleted, I recommend you to use

local CollectionService = game:GetService("CollectionService")

local tag = "Hello"

local seats = cs:GetTagged(SEATS_TAG)

cs:GetInstanceRemovedSignal(SEATS_TAG):Connect(function(seat)
 seats = cs:GetTagged(SEATS_TAG)
end)

cs:GetInstanceAddedSignal(SEATS_TAG):Connect(function(seat)
 table.insert(seats, seat)
end)

3 Likes

Tags don’t get deleted because they aren’t created in the first place. Tags from CollectionService are serialised on the instances themselves. Knowing whether a tag is deleted or not doesn’t matter because CollectionService is designed to create collections based on tags that you add to instances.

If an object is deleted or a tag is removed from the object, it will fire an instance removed signal which passes the object as an argument. You can connect various actions to occur by using GetInstanceRemovedSignal and passing the string of a tag.

local CollectionService = game:GetService("CollectionService")

CollectionService:GetInstanceRemovedSignal("Test"):Connect(function (inst)
    print(inst)
end)

local part = Instance.new("Part")

-- Adding/removing a tag
CollectionService:AddTag(part, "Test") -- Part now has "Test" test
CollectionService:RemoveTag(part, "Test") -- Part loses "Test" tag, fires signal

-- Instance destruction
CollectionService:AddTag(part, "Test") -- Part now has "Test" tag
part:Destroy() -- Part destroyed, fires signal
10 Likes