Can someone explain why this script results in this:
01 | # = Random number |
02 |
03 | # |
04 | # |
05 | # |
06 | # |
07 | # |
08 | - |
09 | 0 |
10 | - |
11 | 0 |
01 | function Add() |
02 | x = First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value |
03 | end |
04 |
05 | First = script.findMean.First --.Value |
06 | Second = script.findMean.Second --.Value |
07 | Third = script.findMean.Third --.Value |
08 | Fourth = script.findMean.Fourth --.Value |
09 | Fifth = script.findMean.Fifth --.Value |
10 | Plus = script.findMean.Plus --.Value |
11 |
12 | -- |
13 |
14 | First.Value = (math.random( 1 , 10 )) |
15 | Second.Value = (math.random( 1 , 10 )) |
How I've got it set up: http://i.imgur.com/unSZNlk.png
You never return a value in Add(), change x = ... to return ...
1 | function Add() |
2 | return First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value |
3 | 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:
01 | First = script.findMean.First |
02 | Second = script.findMean.Second |
03 | Third = script.findMean.Third |
04 | Fourth = script.findMean.Fourth |
05 | Fifth = script.findMean.Fifth |
06 | Plus = script.findMean.Plus |
07 |
08 | function Add() |
09 | return First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value |
10 | end |
11 |
12 | -- |
13 |
14 | First.Value = (math.random( 1 , 10 )) |
15 | Second.Value = (math.random( 1 , 10 )) |
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:
01 | local numbers = { 23 , 5 , 2 , 12 , 10 } |
02 |
03 | function average(table) |
04 | local sum = 0 |
05 | for i = 1 , #table do |
06 | sum = sum + table [ i ] |
07 | end |
08 | return sum/#table |
09 | end |
10 |
11 | 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.