Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Returning # of items in a table as 0, How would i go about fixing?

---RoundSystem
--------------------
MapList = {Template = {SpawnsPerTeam = 1,Time = 1400,Ambient = Vector3.new(150,150,150)}}
Gamemodes = {TeamDeathMatch = {MaxKills = 75,Teams = 2,Time = 5}}


Team1 = {}
Team2 = {}
Time = {Start = 5 * 60,Finish = 0,Current = 0}
GameInfo = {}
function NewRound()
	print(#MapList)
	Map = math.random(1)
	Gamemode = math.random(1,#Gamemodes)
	table.insert(GameInfo,#GameInfo + 1, Map)
	table.insert(GameInfo,#GameInfo + 1,Gamemode)
	print(GameInfo[1])
	print(GameInfo[2])
end

wait(1)
NewRound()


returning print(#MapList) as 0....

1 answer

✓ Accepted answer

In Lua, #MapList only returns the amount of items in the array part of the table. It does not keep track of your dictionary (or "named" or "key-value") entries (such as (in your case): Template).

You need to either keep track of the number of items in your table manually, or use a loop to count them.

You can use a self-defined map_length function, as provided here to count things:


function table.map_length(t)
    local c = 0
    for k,v in pairs(t) do
         c = c+1
    end
    return c
end

a = {spam="data1",egg='data2'}

table.map_length(a)

2

1 comment

0

Thanks! I got a way around it though.

This discussion is preserved as it was. New answers, comments, and votes are closed.