Can someone explain why this script results in this:
# = Random number # # # # # - 0 - 0
function Add() x = First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value end First = script.findMean.First--.Value Second = script.findMean.Second--.Value Third = script.findMean.Third--.Value Fourth = script.findMean.Fourth--.Value Fifth = script.findMean.Fifth--.Value Plus = script.findMean.Plus--.Value -- First.Value = (math.random(1,10)) Second.Value = (math.random(1,10)) Third.Value = (math.random(1,10)) Fourth.Value = (math.random(1,10)) Fifth.Value = (math.random(1,10)) -- print(First.Value) print(Second.Value) print(Third.Value) print(Fourth.Value) print(Fifth.Value) -- print("-") Plus.Value = Add() print(Plus.Value) print("-") print(Plus.Value / 5)
How I've got it set up: http://i.imgur.com/unSZNlk.png
You never return a value in Add(), change x = ... to return ...
function Add() return First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value end
Also, be careful, if you call Add() before First, Second, etc, are defined, it'll error. It's better to define variables before functions.
Here's a rewritten version:
First = script.findMean.First Second = script.findMean.Second Third = script.findMean.Third Fourth = script.findMean.Fourth Fifth = script.findMean.Fifth Plus = script.findMean.Plus function Add() return First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value end -- First.Value = (math.random(1,10)) Second.Value = (math.random(1,10)) Third.Value = (math.random(1,10)) Fourth.Value = (math.random(1,10)) Fifth.Value = (math.random(1,10)) -- print(First.Value) print(Second.Value) print(Third.Value) print(Fourth.Value) print(Fifth.Value) -- print("-") Plus.Value = Add() print(Plus.Value) print("-") print(Plus.Value / 5)
By the way, this really isn't a good way to do it. It's better to use a table rather than use a bunch of NumberValues.
Here's a simple way to get the average of numbers in a table:
local numbers = {23,5,2,12,10} function average(table) local sum = 0 for i = 1, #table do sum = sum + table[i] end return sum/#table end average(numbers) --> 10.4
The only main problem I can see, is that the function is above your variables, and when you are trying to access your variables, it can't, because that part of the script hasn't been loaded yet.
Try putting the function below your variables.