Ok so my game is a round based sword game and im trying (at the moment) make 2 different gamemodes. A Free-for-all and a Team DeathMatch.
To choose a gamemode by math.random should i use a object or string value and if it picks one with a certain name it does the rest of whatever the gamemode is?
ive gotten this far:
local GameModes = GameModesFolder:GetChildren()
local ChosenGameMode = GameModes[math.random(1,#GameModes)]
if ChosenGameMode.Name == 'Free-For-All' then
elseif ChosenGameMode.Name == 'Team DeathMatch' then
For game modes, I would use an object, because then you can hold data about how the game modes work, which is more scalable than just representing game modes as strings and executing them in if statements.
I second what @wafflecow321 is saying. When I use gamemodes, I keep each in a module script, and keep those in a folder. Keeps them nice and organized, and you could pick one of those randomly each time.
That’s the roblox api description of an “object”. In computer science an object is essentially a data structure that holds information about something. This is the fundamental of object-oriented programming. What I was referring to was having like a table with the game mode data, probably like what @tomspell was describing in a module script. If you wanted to really streamline the process you could use object-oriented programming to create new game modes that inherit the data structure from a game mode class.
An easy way is just randomly select a game mode from a table generated from all the available gamemodes and in the table remove the past gamemode. Then simply run the module’s code for the gamemode.
local modes = {"FFA", "TDM"}
local pickedMode = modes[math.random(1, #modes)
if pickedMode == "FFA" then
print("FFA chosen!")
elseif pickedMode == "TDM"then
print("TDM chosen!")
end
Code is self explanatory. It’s basically your version without the use of a value or a part.
Another thing to note, if you do choose to use a table to store all of the modes (like @wayIxn said) , you can also store other information about that game mode using sub tables. This can make it easier for you to edit the code for future preferences. I will give you an example of this.
Using two tables
local modes = {
"FFA";
"TDM";
}
local modeDetails = {
["FFA"] = {
["Time"] = 60; --Time per round.
["Respawn"] = false; --Whether the players in-game can respawn.
};
["TDM"] = {
["Time"] = 180; --Time per round
["Respawn"] = true; --Whether the players in-game can respawn.
};
}
With these two tables, it will much easier to navigate and edit your code. Here’s an example of how you would use it.
*Using the two tables
--INSERT THE TWO TABLES HERE
local randomMode = modes[math.random(#modes)]
if randomMode == "FFA" then
--Load players with items and such
local waitTime = modeDetails[randomMode]["Time"]
local canRespawn = modeDetails[randomMode]["Respawn"]
wait(waitTime)
end
Ok So i think i did it right. No errors which is great. One Thing. Would this be the way to use the Respawn Thing?
local GameModes = {
"FFA";
"TDM";
}
local ModeDetails = {
["FFA"] = {
["Time"] = 5; -- The GameModes Length in Seconds
["Respawn"] = false; -- If they can Respawn or Not
};
["TDM"] = {
["Time"] = 5; --The GameMode's Length in Seconds
["Respawn"] = true; -- If they can Respawn or Not
};
}
local PickedMode = GameModes[math.random(#GameModes)]
if PickedMode == "FFA" then
local GameTime = ModeDetails[PickedMode]["Time"]
local CanRespawn = ModeDetails[PickedMode]["Respawn"]
print("Free For All")
Status.Value = 'Free For All Chosen'
if CanRespawn == false then
-- Sends the Player Back to the Lobby After Death
end
wait(GameTime)
elseif PickedMode == "TDM" then
local GameTime = ModeDetails[PickedMode]["Time"]
local CanRespawn = ModeDetails[PickedMode]["Respawn"]
print("Team DeathMatch")
Status.Value = 'Team DeathMatch Chosen'
if CanRespawn == true then
-- Code for Making the Player Respawn
end
wait(GameTime)
end
end
I don’t believe there is a best way to do this. What you have right now is really good! It’s your preference with how you type your code. Keep up the good work.