I have multiple functions that I want it to be called randomly in my script. However, when I actually insert it inside the table, the function starts to run and I can`t find a way to stop it. I am kind of thinking to do it like this.

local function mission1()
	--Run
end
local function mission2()
	--Run
end
local function mission3()
	--run
end

local missionTable = {mission1,mission2,mission3) --Somehow the function runs in this line as ordered, which is unwanted because it takes so long for every function to finish running.

local pickedMission = math.random(1,#missionTable)

missionTable[pickedMission]

. However, when I use this code, the function starts to run when I am inserting the function into the table. Is there a way to call these functions without having to run when inserting into the code?

I could also think of this way, but this looks very inefficient and overwhelming if I have tons of function to pick from.

local function mission1()
	--Run
end
local function mission2()
	--Run
end
local function mission3()
	--run
end

local missionPicked = math.random(1,3)
if missionPicked == 1 then
	mission1()
elseif missionPicked == 2 then
	mission2()
elseif missionPicked == 3 then
	mission3()
end

2 comments

0

why don't you just put a random number generator in a single function, that way you won't need a table for your random number generator and it will save like 20 lines of code.. I am talking about extra parameters like "local function mission(missionBool1,missionBool2)"

0

so i will pass the random number in parameter and check if the random number is 1 call mission1 and if it`s 2 call mission2?