✓ Accepted answer
How you structure a game is subjective. I can tell you how I would go about this, but it may be confusing. Some people don't even use modules, so it's really how you want to do it. There are definitely better ways than others, but this is my way.
https://gyazo.com/9880f72f86b1936e22e7bf4a61eb2ee4
I would have a Main game Script in ServerScript service. This script handles the Players who join and whether or not the minimum count has been hit to start a new game.
Main
local MainModule = require ( script:WaitForChild('Main') )
local MinimumPlayers = MainModule.MinimumPlayersRequired
while true do
local NumPlayers = #game:GetService('Players'):GetPlayers()
if NumPlayers >= MinimumPlayers then
if not MainModule.GameStarted then
MainModule.Start()
end
else
local Difference = MinimumPlayers - NumPlayers
print ( ('%s more %s required to start the game.'):format(Difference, Difference > 1 and 'players' or 'player') )
end
wait(1)
end
I would have a MainGameModule that holds shared functions that each SubGameModule can use as well as settings.
local module = {}
module.__index = module
module.GameStarted = false
module.MinimumPlayersRequired = 2
module.Playing = {}
local SubGames = {}
for i,v in pairs ( script:GetChildren() ) do
table.insert ( SubGames, setmetatable ( require ( v ), module) )
end
function module.CollectPlayers()
for _, Player in pairs ( game.Players:GetPlayers()) do
table.insert ( module.Playing, Player )
end
return module.Playing
end
function module.CountDown(Count)
for i = Count, 0, -1 do
print ( "CountDown", i )
wait(1)
end
end
function module.Start()
local RandomGame = SubGames[math.random(#SubGames)]
local Playing = module.CollectPlayers()
module.GameStarted = true
RandomGame.Start(Playing)
module.CleanUp()
module.GameStarted = false
end
function module.CleanUp()
for i,v in pairs ( module.Playing ) do
if v and v.Parent then
v:LoadCharacter()
end
table.remove ( module.Playing, i)
end
module.Playing = {}
end
return module
I would then have the SubGameModules
local module = {}
function module.SpawnPlayersToMap()
for i,v in pairs ( module.Playing ) do
print ( ('Spawn %s to map'):format(v.Name) )
end
end
function module.Start()
module.SpawnPlayersToMap()
module.CountDown(10)
end
return module
2 comments
I just made the game mode modules parented to the server script, I don't see why I would need a main module with so much of the code already done in the server script. Thank you for the help anyways!
Like I said, subjective. I do it to keep everything separated and clean. Good luck!