Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can i call a function at random?

Asked by
NexeusX 137
8 years ago

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

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

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.

Ad

Answer this question