# Help with averages

Hello, I want to get the average rate of increase of money over a period of 1 second. I tried doing something simple and small to get the average but I don’t think it is returning the average

``````local Table = {}

local Count = 0

repeat
Count += 1
table.insert(Table, Count)
wait(1)
until Count == 5

local Average = 0

table.foreachi(Table, function(_, Value, ...)
Average += Value
end)
print(Average)
warn(Average / table.getn(Table))
``````

it returns 3 when I want the average to be 1 I don’t know if my equations are wrong but I would like some help in finding the correct answer.

My rate of money increase will not be linear

1 Like

It’s doing exactly as you want though, Everytime the `repeat until` loop runs, it increases the count variable by 1 and adds it to the table. So in the end you’ll have a table that contains 5 numbers

``````{1, 2, 3, 4, 5}
``````

Adding them together gives 15, 15/5 (5 is the length of your table) is 3. Meaning the average is correct i nthis case and is an issue with your setup

You probably need to change the way you’re doing your code to achieve what you want

So my money is going up every frame or close to that and is not going up linearly it is going up my random amounts.

How would I get the average amount of money increase per second, and how could I get the predicted amount of money I would have in the next 10 seconds from that average?

For example:

``````local Table = {}

local Average = 0
table.foreachi(Table, function(_, Value, ...)
Average += Value
end)
print(Average / table.getn(Table))
end)

while true do
table.insert(Table, time())
wait(1)
end
``````

This code is not returning an average of 1, but it clearly should?

I think your table based approach may be an issue, are you required to use a table? If not, just store the money in a variable, and before increasing it, get the current time. Then after it has finished increasing, you can get the time passed by subtracting the before time with os.time(), dividing it with the money gives the average

Example

``````local money = 0

local beforeTime = os.time()

repeat
money += 1
until money == 5

local timePassed = os.time() - beforeTime

print(money / timePassed)
``````

I did this and it correctly printed 1, then if you want it to give the money you’d get after 10 seconds, just multiply 10 by the rate

Yes my money is stored in a int value but as I said before my money is updating depending on the speed you are walking so it’s updates are not linear and there updates are not every 1 second they are every frame to provide a smooth effect.

So the question still stands, how (using this method) would I be able to get the average amount of money earned every second

If you’re updating the money every frame then you could use the renderStep parameter most step events give you, you add it with the previous amounts until it is over 1, then you subtract the previous amount of money with the current amount that you have, multiply it by the step as it can be over 1 in some cases (though not sure if that’s required and you can probably ignore it), and you get the average amount that second gave you

Example

``````local money = --Wherever the money intvalue is stored

local currentStep = 0
local moneyGivenInSecond = 0

RunService.Heartbeat:Connect(function(step)
local moneyToGive = --How you plan on giving the money
money.Value += moneyToGive
moneyGivenInSecond += moneyToGive

currentStep += step
if currentStep >= 1 then
print("You have made " .. (moneyGivenInSecond * currentStep) .. " In 1 second")
currentStep = 0
moneyGivenInSecond = 0
end
end)
``````

If you want a bit more accuracy over time, store the averages given per second in a table and then get the sum of them all and divide it by the length of the table

Not sure if that’s w hat you exactly wanted

I think I got it working myself now.

Script1:

``````local Increase = 10

while true do
workspace.Time.Value += 1
end
``````

Script2:

``````local Time = workspace.Time
local Old = tick()

local Original_Number = Time.Value

Time:GetPropertyChangedSignal("Value"):Connect(function(...)
if tick() - Old >= 1 then
local New_Number = Time.Value

local Increase = New_Number - Original_Number
Original_Number = Time.Value

Old = tick()
end
end)
``````