Better way of making minigame code?

so Im making a minigame based game, and I currently have a working “minigame selector” the way it works is the following:

local Rand = math.random(1, 2)

if Rand == 1 then
--call module script event 1
elseif Rand == 2 then
--call event 2
end

the problem is that I plan on having a lot of minigames, and I do not want to write if statements for every one. so is there a way to more efficiently check Rand and then call the module script, or should I keep going with my if statements? Thanks in advance!

first way I think of doing it is making the options variables, then the game calls that variable and the variable can tell the game to select that minigame

But wouldn’t I still have to write out all the if statements to tell the game what variable to call?

no, just set all the variables and use math.random to pick one

var = math.random(the variable it picked)
minigame = var

something like that.

A really easy way is doing math.random to pick a map and inside that map is a Modulescript that runs the minigame code for that map. This is the easiest way to make it simple and clean.

One simple way you could do this is by making each function name within the module script have something along the lines of “Minigame1” or “Minigame3” and so on. After this, what you can do is call these minigame functions without having several different lines:

local RandomNumber = math.random(1, 2) -- Generate random number

local ModuleFunction = ModuleScript["Minigame"..tostring(RandomNumber)] -- Since module scripts are tables, functions of said module script are also indexs, meaning you can index module functions.
-- We do this by concatenating the Minigame string together with the random number we got, and index that on the module script.
ModuleFunction() -- Call the function and pass parameters in here.
-- Your module script

local Module = {}

function Module.Minigame1()
    print("Minigame 1 has been picked!")
end

function Module.Minigame2()
    print("Minigame 2 has been picked!")
end

return Module

While this does remove some possible naming systems, you can still implement those if you get a little complex.

I do hope this helps you!

1 Like

I hate using remote events if I don’t have to. Modules scripts are the easiest way to make your code minimized in the main script AND you can do alot more.

You could make this even easier by renaming every module script the same name and finding it inside of the map! :slight_smile: