How to add/combine scores from a table?

I have several scores that are within a table.
How would I combine them or add them up for a grand total?
My example below…
Thank you,
Tony

[code]
local level = {}

level[1]= {}
level[1].score=“200”

level[2]= {}
level[2].score=“300”

level[3]= {}
level[3].score=“400”

level[4]= {}
level[4].score=“1000”

[/code] [import]uid: 169633 topic_id: 31872 reply_id: 331872[/import]

In this case, you’re adding non-indexed tables to the “level” table. So, you should just loop through the level table by count like this:

local totalScore = 0  
for i=1,#level do  
 totalScore = totalScore + level[i].score  
end  

You also should declare your level scores as integers, not strings… remove the quotes from around them so the values can be added mathematically.

Brent [import]uid: 9747 topic_id: 31872 reply_id: 127176[/import]

Perfect!

Thank You, Brent! [import]uid: 169633 topic_id: 31872 reply_id: 127179[/import]

In this case, you’re adding non-indexed tables to the “level” table. So, you should just loop through the level table by count like this:

local totalScore = 0  
for i=1,#level do  
 totalScore = totalScore + level[i].score  
end  

You also should declare your level scores as integers, not strings… remove the quotes from around them so the values can be added mathematically.

Brent [import]uid: 9747 topic_id: 31872 reply_id: 127176[/import]

Perfect!

Thank You, Brent! [import]uid: 169633 topic_id: 31872 reply_id: 127179[/import]