CollectionService:AddTag causing memory leaks?

So I am reading up on CollectionService as I want to use it to tag players rather than using :FindFirstChild(“Humanoid”) twice to detect if a hit part is a player or not for performance reasons. But I found this:

I won’t be adding events to the tags I put on player’s character, I will only use the :AddTag function to the BaseParts within the player and any BaseParts added later.
Would I need to remove these tags every time the player dies and the limbs are destroyed using :RemoveTag or is this not necessary?

If you set the tag to the player’s character or any child of it, then every time a player dies and respawns, the tag is removed.

If you don’t want that, simply tag the player itself and not the player’s character.

1 Like

But would this cause memory leaks? Since I wont be removing the tags (preferably)

In all honestly, I have no certainty, but I doubt it would. If you’re reassigning a tag with the same name it should replace the old tag anyways, not have a duplicate tag.

1 Like

Tags don’t leak memory, they’re just strings serialised onto instances. This excerpt is giving the usual spiel about avoiding memory leaks: destroy what you don’t need, disconnect events you don’t need. Clean up after yourself, leave in only things you’re using and get rid of the rest.

When an instance is destroyed, it will automatically have the tag removed and will call any functions listening to an InstanceRemoved signal. If you need the tag for the duration of the game, then you aren’t wasting idle resources and are actively using that connection, so you can leave it be.

3 Likes