Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Arithmetic operations in a table?

For my game i want to calculate the average of the numbers found in a table. So to do this, I have to add all of them and divide it by the number of values. I know how to do that last part (#table), but I have no idea where to start to be able to add all the values of the table together.

1 answer

well you could just do this:

local values = {10,20,40,5,12,34,78} -- change the values in here

function average(array)
	local current_value = 0

	for _,number in pairs(array) do
		current_value = current_value + number
	end

	return current_value/ #array
end

local aver = average(values)
print(aver)

change the values in the values table to the desired values

This discussion is preserved as it was. New answers, comments, and votes are closed.