The code below runs every function that's inside 'FunctionTable' on line 15 even tho I am only calling one of the functions using FunctionTable[counter]. Instead of running every function at once, how do i run a single function in the table for every time a player clicks a ClickDetector? Also, is there a better way of doing this?
counter = 1 function round1() -- runs round1() code end function round2() -- runs round2() code end function round3() -- runs round3() code end FunctionTable = { round1(), round2(), round3() } ClickDetector.MouseButton1Click:connect(function() local CurrentRound = FunctionTable[counter] -- This line is running every function in 'FunctionTable' counter = counter + 1 end)
What appears to be happening is that the functions are being called immediately (before the ClickDetector is even activated) and the ClickDetector doesn't actually do anything yet. This is because you're calling the functions in lines 16-18 whereas you want to simply reference them and call them on line 23 or so:
counter = 1 function round1() -- runs round1() code end function round2() -- runs round2() code end function round3() -- runs round3() code end FunctionTable = { round1, round2, round3, } ClickDetector.MouseButton1Click:connect(function() local CurrentRound = FunctionTable[counter] -- This line is running every function in 'FunctionTable' CurrentRound() counter = counter % #FunctionTable + 1 end)
Now FunctionTable
stores a reference to the functions which enables CurrentRound
to refer to one of the functions and run it. (I added % #FunctionTable
so that it automatically wraps to the first function once you've gotten through all the functions.)