Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Finding the mean of a set of numbers

Asked by
RSoMKC 45
11 years ago

Can someone explain why this script results in this:

01# = Random number
02 
03#
04#
05#
06#
07#
08-
090
10-
110
01function Add() 
02    x =  First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value
03end
04 
05First = script.findMean.First--.Value
06Second = script.findMean.Second--.Value
07Third = script.findMean.Third--.Value
08Fourth = script.findMean.Fourth--.Value
09Fifth = script.findMean.Fifth--.Value
10Plus = script.findMean.Plus--.Value
11 
12--
13 
14First.Value = (math.random(1,10))
15Second.Value = (math.random(1,10))
View all 37 lines...

How I've got it set up: http://i.imgur.com/unSZNlk.png

2 answers

Log in to vote
2
Answered by
Bubby4j 231 Moderation Voter
11 years ago

You never return a value in Add(), change x = ... to return ...

1function Add()
2    return First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value
3end

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:

01First = script.findMean.First
02Second = script.findMean.Second
03Third = script.findMean.Third
04Fourth = script.findMean.Fourth
05Fifth = script.findMean.Fifth
06Plus = script.findMean.Plus
07 
08function Add() 
09    return First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value
10end
11 
12--
13 
14First.Value = (math.random(1,10))
15Second.Value = (math.random(1,10))
View all 37 lines...

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:

01local numbers = {23,5,2,12,10}
02 
03function average(table)
04    local sum = 0
05    for i = 1, #table do
06        sum = sum + table[i]
07    end
08    return sum/#table
09end
10 
11average(numbers) --> 10.4
0
Thank you! I wasn't aware that tables were a thing! RSoMKC 45 — 11y
2
Tables are amazing! You can do anything with tables. See http://wiki.roblox.com/index.php/Table Bubby4j 231 — 11y
Ad
Log in to vote
0
Answered by 11 years ago

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.

0
That's wrong, the function Add() is called AFTER the variables are defined. Lua isn't compiled :) Bubby4j 231 — 11y
0
Really? If that is wrong, then why did you suggest the same exact thing? Don't be a hypocrite . AmericanStripes 610 — 11y
0
I didn't suggest the same thing. You were implying that there would be an error because the variables were defined after the function. The fact is that it doesn't matter, as long as the variables are defined before the function is actually called. Bubby4j 231 — 11y
0
Test this script, it works: http://pastebin.com/BCiU8GGR Bubby4j 231 — 11y
View all comments (2 more)
0
Lua actually is compiled. It's compiled at run time. Functions and variables must be defined before you use them. (But you don't need to actually set them to anything) User#11893 186 — 11y
0
By compiled I meant compiled to machine code. Bubby4j 231 — 11y

Answer this question