Hello, everyone! I have a problem where I want to make a random minigame for the players every round. I can already make the minigames, but there is one problem I’m having. I’m trying to make the minigame without using repetitive if statements.
I used math.random() to generate a random number but writing:
if num == 1 then
function()
if num == 2 then
function()
if num == 3 then
function()
Is too repetitive. How would I go about making this in an efficient way? I’m sure that games with lots of minigames don’t use 100 if statements, so someone helping me would be appreciated!
P.S: I had some ideas like putting all the maps into a folder called “Minigames” and then putting scripts inside the maps that get enabled when their game is called. Instead of using if statements, I would loop through the maps folder and choose a random minigame. However, too many scripts is annoying to work with, so I’m trying to use functions all in one script. Once again, thank you!
Hey, I am pretty sure there are some good online tutorials you can follow. Just try not to copy and paste make sure you understand and learn the script before you use it.
If You Have Multiple Maps Then You Can Do It Like This…
First Create A Folder Named Map In Replicated Storage And Place All Your Maps In There.
2.Now Create A Script In ServerScriptService And You Can Write Something Like This
local MapsFolder = game:GetService("ReplicatedStorage"):WaitForChild("Map")
local Maps = MapsFolder:GetChildren()
local ChooseMap = Maps[math.random(1,#Maps)]
local Clone = ChooseMap:Clone()
Clone.Parent = game.Workspace
The thing about this script is that it’s just choosing a map. I can already choose a random map and clone it, I just don’t know how to have a function run if that certain map is chosen
LIke rules. For example, one game can be a murder mystery where one player is handed a gun and another game can be musical chairs. I just don’t know how to run the murder mystery script if the murder mystery map is chosen.
I thought of enabling scripts that are children of the map, but I don’t want too many scripts
game.Players.PlayerAdded:Connect(function()
local Result = math.random(1,3)
local FinalNumber = 0
FinalNumber = Result
if FinalNumber == 1 then
end
if FinalNumber == 2 then
end
if FinalNumber == 3 then
end
end)
That’s another thought I had but I wasn’t sure of how I would do it. I can make the functions in the module script, but how would I call it in the server script if a certain map is chosen. For example:
local minigames = require(ModuleScript)
Minigames[#maps]
So I understand what you’re saying, but wouldn’t this require a module script for each minigame? Would that be okay for the game or ruin the performance?
Also, wouldn’t this bring up the problem of multiple if statements I had before?
No, It Won’t Harm The Game Because When The Round Is Over The Clone Will Be Destroyed Which Will Also Destroy The Module Script So. It Shouldn’t Lag The Game.
Yes It Will Take Multiple If Statements. But It Shoudn’t Lag The Game Too Much.