GetTags does not work as intended

I’ve noticed GetTagged does not work as intended. No clue when this started, cause I’ve only just starting using CollectionService, but

local function AddTag(object, tagType)
	print(object, tagType) -- prints NinjoOnline Player
	CollectionService:AddTag(object, 'Interaction')
	CollectionService:AddTag(object, tagType)
end

local function SetupTags()
	-- Get all player tags
	for _, v in pairs(Players:GetPlayers()) do
		AddTag(v, 'Player')
	end
end

local function CheckTags(object)
    print(object) -- NinjoOnline

    local Tags = CollectionService:GetTags(object)
	print(Tags, #Tags) -- prints table 0

	-- Find the specific tag
	local Tag
	for i, v in pairs(Tags) do
		print(i, v) -- Nothing prints
		if v ~= 'Interaction' then
			print(Tag)
			print(v)
			Tag = v
		end
	end
end

SetupTag()

script.Parent.Activated:Connect(function()
    CheckTags(game.Players.LocalPlayer)
end)

GetTags should return an array of strings, the strings being

{'Player', 'Interaction'}

So now clue why (or how) it’s returning nothing, ie, bug

You likely need to re-set the tags on the objects. There was an issue recently that caused textboxes (e.g. textbox in Tag Editor plugin) to put extra characters at the end of the tags: Hitting enter in a textbox places a carriage return before finalizing input (broken again)

Also, in the future, please add a self-contained repo to your post. This is just code for handling tags, it doesn’t actually show the problem because you’re not including any .rbxl with example objects that the issue occurs on.

1 Like

I’m not using any plugin/tag editor tho

You should probably include a full repro then. (.rbxl file that shows issue on Play / putting something in cmd bar)

1 Like

In your example you call SetupTag but there is no function with this name. Please could you include the original snippet to show us exactly what you’re doing?

2 Likes

There’s a few issues with your repro script:

  1. You are calling AddTag, but this appears to be a client-side script due to the use of LocalPlayer and an Activated callback.
  2. The repro doesn’t seem to show any issues when I run it on my end.
  3. After making modifications to your script so that it will actually run, I do not observe the behavior you’re running into.

Server script

local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")

-- Called from the server side so that the changes will be replicated.
local function AddTag(object, tagType)
	print("AddTag", object, tagType) -- prints NinjoOnline Player
	CollectionService:AddTag(object, 'Interaction')
	CollectionService:AddTag(object, tagType)
end

local function SetupTags()
	-- Get all player tags
	for _, v in pairs(Players:GetPlayers()) do
		AddTag(v, 'Player')
	end
end

-- Wait until local player is actually logged into the server.
wait(1)
-- Call a function that exists.
SetupTags()

Client Script

local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")

local function CheckTags(object)
	print("CheckTags", object) -- NinjoOnline
	
	local Tags = CollectionService:GetTags(object)
	print("CheckTags tags = ", Tags, #Tags) -- prints table 0
	
	-- Find the specific tag
	local Tag
	for i, v in pairs(Tags) do
		print(i, v) -- Nothing prints
		if v ~= 'Interaction' then
			print(Tag)
			print(v)
			Tag = v
		end
	end
end

-- Wait until server setup is complete.
wait(3)
CheckTags(game.Players.LocalPlayer)

Output

  AddTag Tiffblocks Player
  CheckTags Tiffblocks
  CheckTags tags =  table: 0x687c13b05e4de166 2
  1 Interaction
  2 Player
  nil
  Player