GetTagged return an empty table

I have 2 tags (SwordFight and GunFight) and a script that chooses a random tag, then chooses a random map with the tag.

The problem is that when I use GetTagged to get a random map with the chosen tag, it returns an empty table.

local CollectionService = game:GetService("CollectionService")

local Tags = CollectionService:GetAllTags()
local RandomTag =  Tags[ math.random( #Tags ) ]
print(RandomTag) -- Print a random tag

local Maps = CollectionService:GetTags( RandomTag ) 
print(Maps) -- Print {}
local RandomMap =  Maps[ math.random( #Maps ) ] -- Give an error because it's an empty table 

I tried to add toString to the random tag, but it did not help.

Have you double checked you tagged the maps?

1 Like

I solved the issue. Rather than using GetTagged, I was using GetTags. This was a silly mistake by me sorry.

1 Like

Glad you managed to sort it! (:

1 Like

I got some weird bug

function RoundService:PickAGameMode()
    local Tags = CollectionService:GetAllTags()
    return tostring( Tags[math.random( #Tags )] )
end

function RoundService:PickAMap(GameMode) 
    local Maps = CollectionService:GetTagged(GameMode)
    return tostring( Maps[ math.random( #Maps ) ] )
end

local GameMode = RoundService:PickAGameMode()
local Map = RoundService:PickAMap(GameMode)
print(GameMode, Map)

the code print TagEditorTagContainer TagList

1 Like

TagEditorTagContainer looks like GetAllTags is giving you some unexpected tags. Looks like perhaps a Tag Editor plugin is using “TagEditorTagContainer” internally? As “TagList” (I assume a folder) is tagged with “TagEditorTagContainer”, you’re getting that as an output from your print statement.

You might want to consider doing something such as

local gamemodeTags = {...}


function RoundService:PickAGameMode()
    return tostring( gamemodeTags[math.random( #gamemodeTags )] )
end

This way you won’t accidentally catch any irrelevant CollectionService tags.

1 Like

In the Tag Editor it says there is unknown Tags
image
image
I will just do a if statement to skip this tag

Thanks for your Help!

1 Like

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