Number of Maps in a table returns zero, even if there's one or more maps in said table

Hi, everyone!

Recently, while attempting to add more gamemodes to Redshift Arena, I’ve ran into an issue where for all gamemodes after Team Deathmach, it always returns 0, no matter how many maps are in the table.

local CompatibleMaps = {
	CTF = {},
	TDM = {},
	INF = {},
	MTTDM = {},
	FFA = {},
	CNQST ={},	
}

for i, map in pairs(game.ServerStorage.Maps.CoreMaps:GetChildren()) do
	if map.CompatibleGamemodes then
		for x, gamemode in pairs(map.CompatibleGamemodes:GetChildren()) do
			if CompatibleMaps[gamemode.Name] then
				table.insert(CompatibleMaps[gamemode.Name],x,map)
				warn("Number of compatible maps: "..#CompatibleMaps[gamemode.Name], "Gamemode: "..gamemode.Name, "Map: "..map.Name)
			end
		end
	end
end

Here’s how the maps are structured:

image

If anyone has any solutions to this, please let me know!

1 Like

What do you mean by saying “it always returns 0”. Where in your code is it returning zero, and what is it supposed to return?

So let’s say, you have two maps that are compatible with TDM, CTF, and FFA.

Based on what gamemodes they’re compatible with, that map will be added to the corresponding table in the script.

The warn function at the bottom is supposed to get the “children” of said table and display the number of maps in that table.

For example, since there’s 2 maps that are compatible with CTF, TDM, and FFA, the

#CTF
#TDM
#FFA

Are all supposed to return 2, since two maps are compatible with all three.

However, somewhere I messed up because even if FFA has 2 maps in it, it’ll always return 0, even if it was successfully added to the table, to my knowledge at least.

EDIT
I have discovered that if you have more than 2 gamemodes associated with a map, it cancels out everything after the 2nd gamemode. For example,

CTF,
TDM,
FFA

FFA straight up gets omitted and I have to manually create a map specifically for that gamemode.

1 Like

I believe the problem is here:

table.insert(CompatibleMaps[gamemode.Name],x,map)

table.insert takes up to 3 arguments.
The first one being the table that you’re inserting the value into, the second one being the value you’re inserting, and the third one is the position, which defaults to #n + 1

you’re using i,map in pairs, which means i is the index and map is the value
but you’re using map in the third argument

x is the index of the second loop
for x, gamemode in pairs(map.CompatibleGamemodes:GetChildren()) do

this leads me to believe that you can solve the problem by flipping it to
table.insert(CompatibleMaps[gamemode.Name],map,x)

because x is the index

You can also do
table.insert(CompatibleMaps[gamemode.Name],map)
because you don’t actually need the third argument

1 Like

It seems when you are doing warn() it is inside of the loop, the values are outputted before everything has been added to the list. This is causing your code to spew out way more information than you want.

I created a separate loop that indexes the table after each value has been added and prints the values, it seems to be accurate.


local CompatibleMaps = {
	CTF = {},
	TDM = {},
	INF = {},
	MTTDM = {},
	FFA = {},
	CNQST ={},	
}

for i, map in pairs(game.ServerStorage.Maps.CoreMaps:GetChildren()) do
	if map.CompatibleGamemodes then
		for x, gamemode in pairs(map.CompatibleGamemodes:GetChildren()) do
			if CompatibleMaps[gamemode.Name] then
				table.insert(CompatibleMaps[gamemode.Name],1,map)
			end
		end
	end
end

for gamemode,maps in pairs(CompatibleMaps) do
	local compatibleMaps = 0
	local strMaps = {}
	for i,map in pairs(maps) do
		compatibleMaps = compatibleMaps + 1
		if i > 1 then
			table.insert(strMaps,#strMaps+1,", ")
		end
		table.insert(strMaps,#strMaps+1,map.Name)
	end
	warn("Number of compatible maps: ".. compatibleMaps, "Gamemode: "..gamemode, "Maps: ".. table.concat(strMaps))
end 

Let me know if this is what you were looking for.

Your second solution worked to solve my issue! Seems I had been using an unneeded variable that would actually prove to hurt with more than two gamemodes included in a single map.

Thank you and @Vmena for helping solve this!

2 Likes