I was just fooling around in Roblox, when I wanted to make a script that averaged some numbers from a variable that shifted value. Here is that code:
local CookyVar = math.random(1,100) local timesRan = 0 local function convToAverage(number1, number2, number3, number4, number5) end while 1 == 1 do CookyVar = math.random(1,100) print(CookyVar) wait(1.5) timesRan = timesRan + 1 print(timesRan) end
The bottom half is just a test script, but there is something in there. The timesRan variable exists to tell how long the script has been running for, and to take those five values from the CookyVar variable and make them into parameters for functions. How can I do that? This script doesn't really do anything revolutionary, but it would nice to know for future use.
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(...) -- '...' is how you declare a variadic function. print(...) -- This will print the amount of arguments we pass end Test(1,2,3) -- prints '1', '2', '3' Test(1,2,3,4,5) -- prints '1', '2', '3', '4', '5' Test('hello','world') -- prints '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(..., ...) -- This will error. You can only use this infinite marker once per function, and this would be considered a syntax error. print(...) end
Invalid example #2
local function Test(...,x,y) -- This is also a syntax error. You cannot have arguments after an infinite marker (this defies logic we don't even understand yet) 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,...) -- This will work, since we can declare x and y existing before our variadic place holder print(x,y,...) end Test(1,2,'hi','there') -- Runs the function with x=1, y=2, and ... being 'hi' and 'there' (Note that '...' is a set of unpacked arguments)
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) -- And we should know how this works by now...
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 = {...} -- Now our arguments are stored in a list. 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 -- This will be how much our numbers add up to -- Numeric for loop time! for i = 1,#Nums do -- Iterate through the array's length Total = Total + Nums[i] -- Add the current index to the total end -- Now we divide the total number by how much content our list stores, to get the average. Total = Total / #Nums -- And finally, return the value to the function call. return Total end print(AverageNumbers(5,5,5,5,5)) -- Average is 5 print(AverageNumbers(2,4,2,4,2)) -- Average is 2.8
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 -- Set a list for 5 random values local Randoms = {} -- Store them via for loop for i = 1,5 do Randoms[i] = math.random(1,100) end -- Return them, but using the unpack function. You can find a document for this function using the link I gave above, in my explanation. 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!