How do I add all the values in a table together

local Example = {
	
	34,34,23,45
	
}

How would I go about adding all those to get one big sum

local value = 0
for i,v in pairs(Example:GetChildren()) do
value = value + v
end
print(value)

that’s what I could think of right now. Maybe I’m wrong and it doesn’t work

3 Likes

Alright let me test this out really quickly

It worked nicely thanks for the reply, I guess this is actually a suitable way to do it thanks

1 Like
local Example = {34,34,23,45}

local Sum = 0

table.foreach(Example, function(i, v)
Sum = Sum + v
end)

print(Sum) --> 136
1 Like

Example is a table, not an instance. You can’t use :GetChildren() on a table. The loop is enough.

Here is the fixed script from @Dolphin_Worm’s one

local value = 0
for i,v in pairs(Example) do
value = value + v
end
print(value)

5 Likes

i recommend using ipairs instead, but that should work