Good evening,
What I am trying to achieve is a code that will pick a random function out of a list of functions or a table, I do not know a current best and simple way to easily set up a random function selector.
My intentions are to pick a random function and that function will run and in my case i am making a football game the random function will run the code inside of it, so so far all i need to do is find a way for the random function is to be selected and then the function will run.
local function Play1()
print("this play is play1")
end
local function Play2()
print("this play is play2")
end
is there a way to put these function in a table and then a code will run and randomly select one of the 2? and so on and so forth if I add more functions, It will then select a random function.
local function func1() end
local function func2() end
local function func3() end
local listOfFunctions = {
func1,
func2,
func3
}
local randomFunc = listOfFunctions[math.random(#listOfFunctions)]
I received this error in output, I made it so I knew which function i knew got picked and here is the whole code, seems something is wrong with the random function line. (I didn’t run the code)
local function func1()
print("func1")
end
local function func2()
print("func2")
end
local function func3()
print("func3")
end
local listOfFunctions = {
func1(),
func2(),
func3()
}
local randomFunc = listOfFunctions[math.random(#listOfFunctions)]
Inserting as in having them in the script itself? I don’t quite understand. I have them in the script is there something I need to change other than having them just like this?
local function func1()
print("func1")
end
local function func2()
print("func2")
end
local function func3()
print("func3")
end
local listOfFunctions = {
func1(),
func2(),
func3()
}
local randomFunc = listOfFunctions[math.random(#listOfFunctions)]
randomFunc()