[Serious] CollectionService:GetTagged() returns duplicate items

Problem

When calling CollectionService:GetTagged(), duplicate objects are returned which causes chaos in the game, especially if events get attached to those objects. This happens in both Studio and in a live game. For instance, if there’s four items in the game that have a tag, the API call returns eight items.


Additional Information

Parameter Value
Problem Area Engine
Problem Component API
First Noticed 27 Jan 2025
Priority High
Impact Serious
Annoyance Level Moderate

The beta features that I have enabled are as follows:

  • Assistant Preview
  • Texture Generator

As for plugins, I have a bunch that are installed, but very few are enabled. The ones that are enabled are listed below with links to their details page on the Roblox store website:

Note: I tried disabling all plugins, but it still happens in Studio.

The live game can be found here: https://www.roblox.com/games/15311253782/Bug-Demonstations


Expectations

What I expect to happen is that CollectionService:GetTagged() does not return duplicate items.


Visuals


Reproduction

To reproduce the issue, perform the following steps:

  1. Load the attached file into Studio.
  2. Run it.
  3. Observe results.

Files

Engine - CollectionService.rbxl (65.1 KB)

if you do


for _, object in ipairs(collectionService:GetTagged("TabButton")) do
	if object:FindFirstAncestorOfClass("PlayerGui") then
		print(object.Name)
		printToFrame(object.Name)
	end
end

it gets fixed

This is the expected behavior.

GetTagged returns all of things with the tag in the place, regardless of where they are. The buttons with the tag in this case are present in the player’s Gui… and in the starterGui template it was cloned from.

There are two solutions:

  • Filter the results as described above
  • Generate the Gui or add the tags at runtime via code

If you use a “binder” pattern for tag handling you can build that filtering code into the binder so that it doesn’t have to be repeated for the various pieces of tag handling code you have.

3 Likes

I do not know why I didn’t see that earlier. But it does make sense now that I think about it. I’ve added a check to see if the GUI is under the player or not, and now it’s working. This can be marked as solved.

It’s not unusual for people to be surprised by this gotcha: When you don’t see the contents in the 3d scene it’s easy to forget that storage like StarterGui / ReplicatedStorage is still part of the main game hierarchy and affected by general APIs.

1 Like

I know it clones over, but it didn’t dawn on me that’s where it was getting the additional objects from. Something to keep in mind.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.