Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I have a question, how to call a random function from a table?

Im working on a minigames game and I think the best solution is to make a table with functions to load map, teleport players, play minigame round system, show winners and end the game (separately for all minigames) but I don't even know how to do it (only the table and choose random function) I sincerely thank you for each answer

1 comment

1

There are multiple ways to go about this. Try looking online before asking a question.

1 answer

✓ Accepted answer

You can simply call the functions directly from the table, for example:

local functionsTable = {
	function() -- index 1
		print("Function index 1 running")
	end,
	function(a,b) -- index 2
		print("Function index 2 printing " .. a+b)
	end,
	function(a,b) -- index 3
		return (a*b)
	end
}

functionsTable[1]() -- prints "Function index 1 running"

functionsTable[2](2,5) -- prints "Function index 2 printing 7"

functionsTable[3](2,2) -- returns 4 

To make more than one minigame, you can create a dictionarie containing functions, so you can select each minigame and create different functions for each

2 comments

0

but when I want to call a random function, can I use functionsTable[math.random(1, #functionsTable)] instead of functionsTable1 ?

0

yes, just don't forget the ()

This discussion is preserved as it was. New answers, comments, and votes are closed.