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

Help with using ModuleScripts to control game??

So, parented to the main, server side script, I have the main module. Code:

local gamemodes = {"Destroy", "Battle"}
local chances = {"90"}
local gamemode
local gameChances = math.random(0,100)
if gameChances < chances[1] then
	gamemode = gamemodes[1]
elseif gameChances > chances[1] then
	gamemode = gamemodes[2]
end

local function startGame()
	require(script:FindFirstChild(gamemode)):Start()
end

Then, parented to that, I have two modules. Destroy and Battle. I'm only using destroy right now. Code:

local gamemodes = {"Destroy", "Battle"}
local chances = {"90"}
local gamemode
local gameChances = math.random(0,100)
if gameChances < chances[1] then
	gamemode = gamemodes[1]
elseif gameChances > chances[1] then
	gamemode = gamemodes[2]
end

local function startGame()
	require(script:FindFirstChild(gamemode)):Start()
end

Then, how can I check to see when the game is finished on the server? Am I even doing this right? I'm very new to Module Scripts.

2 comments

0

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!

0

Like I said, subjective. I do it to keep everything separated and clean. Good luck!

1 answer

✓ 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 
		-- Is there a game running?
		if not MainModule.GameStarted then
			-- No? Start
			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 = {}

-- Make it so each subgame can referance this module
for i,v in pairs ( script:GetChildren() ) do 
	table.insert ( SubGames, setmetatable ( require ( v ), module) )
end

-- inserts player into playing table
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

-- Start game
function module.Start()
	-- Select SubGame
	local RandomGame = SubGames[math.random(#SubGames)]
	local Playing =  module.CollectPlayers()
	module.GameStarted = true
	-- SubGame module should now handle 
		-- everything with the help of this module
	RandomGame.Start(Playing)
	-- cleanup
	module.CleanUp()
	-- new game can be started again
	module.GameStarted = false
end

function module.CleanUp()
	-- re-spawn player and reset playing table
	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 = {}
--	workspace.Map:Destroy()
end

return module

I would then have the SubGameModules

local module = {}

function module.SpawnPlayersToMap()
	-- module.Playing is valid because of the metatable in Main
	for i,v in pairs ( module.Playing ) do
		print ( ('Spawn %s to map'):format(v.Name) )
	end 
end

function module.Start()
	module.SpawnPlayersToMap()
	-- after countdown ends it will return to the main module
		-- and continue after RandomGame.Start()
	module.CountDown(10)
end

return module

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