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.
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)
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.