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

How can i call a function at random?

Ive been making a script that calls functions to start an event in the workspace, but i want to be able to call a function at random, ive tryed making a table and using math.random to see if a could get it to work, ive checked the wiki but im not sure what to look for, could you give some idea how i can do this?

1 answer

✓ Accepted answer

You could do this using a table.

You can store all the functions you want into the table, index it with math.random, and call the function that is returned to you.

Example:

local function1()
	...
end

local function2()
	...
end

local functions = {function1, function2}
local index = math.random(1, #functions)
local randomFunction = functions[index]
randomFunction()

Note that this will error if #functions is less than 1. You could do a check with an if statement if necessary.

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