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

How can I make a table with numbers in it add all of it?

Asked by 5 years ago

So I wanted to know how to make a table add numbers inside of it, for example

local table = {1, 2, 3}

becomes 6 in the end

Ask me more questions if you still don't understand :)

1 answer

Log in to vote
2
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Simply iterate through the table with pairs and add each value to a variable:

local function AddTable(Table)
    local Total = 0
    for _,Value in pairs(Table) do
        Total = Total + Value
    end
    return Total
end

local TableToAdd = {1,2,3}
print(AddTable(TableToAdd)) --> 6
Ad

Answer this question