How to get the total value of a lot of int value?

There is 1 Total “IntValue” and a lot of normal “IntValue”
So i want the value of the Total IntValue be the total of all value of normal IntValue.

Value1 = 1
Value2 = 3
Value3 = 1
Value4 = 4
Value5 = 2
The total value give me 2… and i want to get 11 (The total of all value)

This is only a simple test, I will use it on a folder with +100 IntValue and i want to get the total of them without call them one per one like TotalValue = Value1 + Value2 ect… i dont want that, this is to long.

Here is my script:

while wait(0.1)do
	for _, Child in pairs(script.Parent.Parent.Values:GetChildren())do
		if Child.ClassName == "IntValue" and Child.Value > 0 then
			script.Parent.Value = 0 + Child.Value
		end
	end
end
script.Parent.Value = 0 + Child.Value

Should be

script.Parent.Value += Child.Value

Also why is it in a while loop?

Maybe something like

for _,child in pairs(values:GetChildren()) do
    totalValue.Value += child.Value
end
for i,v in pairs(Values:GetChildren()) do
   TotalValue.Value = (TotalValue.Value + v.Value)
end

Try this, should work.

This is in a While loops just for the test in a baseplate.

totalValue.Value += child.Value

This work but not fully, the total value always need to be the total of all other value so when there is a loop or the total value change when all other value are changed, the total value keep his old value and add all other value again, so the total value is wrong

Oh you want to keep them onupdate?

yes, that’s why the total value must to be a 0 + all value

Do you mean something like this?

while wait(0.1)do
	for _, Child in pairs(script.Parent.Parent.Values:GetChildren())do
		if Child.ClassName == "IntValue" and Child.Value > 0 then
			script.Parent.Value += Child.Value
		end
	end
	script.Parent.Value = 0
end

The thing is, you are resetting it every time. This makes it so that in the end the total value will be only the last value.

This work, thanks
It work only with a while loop and not with Child.Changed, but it’s ok

while wait(0.1)do
	script.Parent.Value = 0
	for _, Child in pairs(script.Parent.Parent.Values:GetChildren())do
		if Child.ClassName == "IntValue" and Child.Value > 0 then
			script.Parent.Value += Child.Value
		end
	end
end
1 Like

Anytime! I would recommend marking a post as the solution so others will know this has been solved!

If you have anymore issues don’t be afraid to make another post!

1 Like