Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
---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

Log in to vote
1
Answered by
doomiiii 112
7 years ago
Edited 7 years ago

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

0
Thanks! I got a way around it though. Abstract_Life 65 — 7y
Ad

Answer this question