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

How do I make a random function?

So I want to figure out how to make random functions, this can be useful to make a game like Horrific Housing.

function One()
       print("1")
end

function Two()
       print("2")
end

function Three()
       print("3")
end

local functions = {One(), Two(), Three()}
math.random(1, #functions)

I'm using strings because it shows whats happening. When you run this, it'll print all the numbers that are there, is there a way to fix this?

1 answer

✓ Accepted answer

You can store your functions invariables like so:

one = function()
       print("1")
end

two = function()
       print("2")
end

three = function()
       print("3")
end

then put those variables in a table:

local functions = { one, two, three }

and pick a random function in that table/call that function

randomFunction = functions[math.random(1, #functions)]

randomFunction()

All together, it would look like this:

one = function()
       print("1")
end

two = function()
       print("2")
end

three = function()
       print("3")
end

local functions = { one, two, three }
randomFunction = functions[math.random(1, #functions)]
randomFunction()

5 comments

0

Can confirm that this does work.

0

he's been online since i posted this so not sure what's up

0

works... should be marked as answer

0

Thank you, I really needed something like this!

0

no problem if you need any more help just ask

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