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

Finding the mean of a set of numbers

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

2 answers

✓ Accepted answer

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

2 comments

0

Thank you! I wasn't aware that tables were a thing!

2

Tables are amazing! You can do anything with tables. See http://wiki.roblox.com/index.php/Table

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.

6 comments

0

That's wrong, the function Add() is called AFTER the variables are defined. Lua isn't compiled :)

0

Really? If that is wrong, then why did you suggest the same exact thing? Don't be a hypocrite .

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.

0

Test this script, it works: http://pastebin.com/BCiU8GGR

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)

0

By compiled I meant compiled to machine code.

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