Okay, based on my understanding you'd like to make a function average 5 numbers, every 7.5 seconds, all in which should be random. First, let's get a few things started.
1) Creating our function
First thing we're going to do, is create the function that will average the number of numeric inputs we give it. I'll explain this as I go:
2) Declaring your parameters
In this case, we can use a variadic function. A variadic function works the same as any other function, with the addition of being able to take in as many arguments as you want to pass to it. This parameter slot can only be used once, and must be used at the end of your parameter list. Here's an example:
local function Test(...)
print(...)
end
Test(1,2,3)
Test(1,2,3,4,5)
Test('hello','world')
Now we have a function that we can pass an unlimited amount of arguments to.
Following the same rules I listed above, I'll go over some invalid examples of declaring variadic functions:
local function Test(..., ...)
print(...)
end
Invalid example #2
local function Test(...,x,y)
print(...,x,y)
end
So that leaves us with creating a variadic function in one of two ways:
a) Declared arguments w/ variadic arguments
local function Test(x,y,...)
print(x,y,...)
end
Test(1,2,'hi','there')
To understand what I meant by unpacked arguments, I suggest visiting this: http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#unpack
b) Completely variadic functions
And this goes back to the first example I showed you, basically looking like this:
local function Test(...)
print(...)
end
Test(1,2,3)
3) Our function's code
Now we need something for the function to run. In this case, we know we're returning only numbers to the function, which will make this next procedure a bit easier:
First, let's put our arguments into an array (or, list in other terms) when we call the function:
local function AverageNumbers(...)
local Nums = {...}
end
3) Math and For loops
Now that we have our template down, we can get to the exciting part; our function's logic. We know our array consists of numbers, and we're obviously going to use a for loop to iterate it. Here's how our code should look:
local function AverageNumbers(...)
local Nums = {...}
local Total = 0
for i = 1,#Nums do
Total = Total + Nums[i]
end
Total = Total / #Nums
return Total
end
print(AverageNumbers(5,5,5,5,5))
print(AverageNumbers(2,4,2,4,2))
4) Implement the function in your game logic
Now we have the function down, and it's ready to be implemented! Let's apply this to a revised version of your while loop for a final product:
local function AverageNumbers(...)
local Nums = {...}
local Total = 0
for i = 1,#Nums do
Total = Total + Nums[i]
end
Total = Total / #Nums
return Total
end
while wait(7.5) do
local Randoms = {}
for i = 1,5 do
Randoms[i] = math.random(1,100)
end
print(AverageNumbers(unpack(Randoms)))
end
And that's it. If you have any questions, let me know. If you don't know much about how for loops work, I suggest checking these sites:
http://wiki.roblox.com/index.php?title=Loops#unpack
http://www.lua.org/pil/4.3.4.html
Hope it helped!
1 comment
Code block fail XD