Trying to make a Round Based Game

Hi, there,

I am trying to make an FPS game and I need some help fixing some bugs.

I have a module script in ReplicatedStorage which handles all functions making the game round based. I also have a script inside ServerScriptService which calls all functions from the module script above.

The bugs I am experiencing are as follows:

  1. The module starts the vote system smoothly but it doesn’t start the round.
  2. I tried to implement a mode system but it doesn’t work at all (no output errors)

I have tried multiple approaches to fix it but it doesn’t seem to work at all.

Your response is highly appreciated.

ModuleScript
(Disclaimer, Long)

local module = {}

local function toMS(s)
 return ("%02i:%02i"):format(s/60%60, s%60)
end

local status = game.ReplicatedStorage.Status

function module.createIntermission(length)
 for i = length,0, -1 do
	status.Value = "Intermission "..toMS(i)
	wait(1)
 end
end

function module.teleportPlayers(players, mapSpawns)

 for i, player in pairs(players) do
	 if player.Character then
		 local character = player.Character

		 if character:FindFirstChild("HumanoidRootPart") then

			 player.Character.Humanoid.WalkSpeed = 16
			 local rand = Random.new()
			 player.Character.HumanoidRootPart.CFrame = mapSpawns[rand:NextInteger(1,#mapSpawns)].CFrame * CFrame.new(0,10,0)

		 end
	 end
  end
end

function module.startRound(length,player,Map)
 local outcome

 game.ServerStorage.GameValues.GameInProgress.Value = true

 for i = length,0,-1 do

	local contestants = {}
	status.Value = toMS(i)

  end
 wait(5)
end

function module.selectMap()

 local Votes = {}

 game.ReplicatedStorage.OtherRemotes.ToggleMapVote:FireAllClients(true)

 for i, v in pairs(game.ReplicatedStorage.Maps:GetChildren()) do
	Votes[v.Name] = {}
 end

 local placeVoteConnection = game.ReplicatedStorage.OtherRemotes.PlaceVote.OnServerEvent:Connect(function(player,mapName)    
	 if Votes[mapName] then

		 for i, playerVotesTab in pairs(Votes) do
			 for x, playerName in pairs(playerVotesTab) do
				 if playerName == player.Name then
					 table.remove(playerVotesTab,x)
					 break
				 end
			e nd
		 end	

		 table.insert(Votes[mapName],player.Name)
		 game.ReplicatedStorage.OtherRemotes.UpdateVoteCount:FireAllClients(Votes)

	 end
 end)

 game.ReplicatedStorage.MapVoting.Value = true
 wait(15)
 game.ReplicatedStorage.MapVoting.Value = false
 game.ReplicatedStorage.OtherRemotes.ToggleMapVote:FireAllClients(false)

 placeVoteConnection:Disconnect()

 local mostVotes = nil
 local mostVoted = nil

 for i, mapTable in pairs(Votes) do

	 local votes = #mapTable

	 if mostVotes == nil then
		 mostVotes = votes
		 mostVoted = i

	 else

		 if votes >= mostVotes then
			 mostVotes = votes
			 mostVoted = i
		 end	
	 end
 end	

 local chosenMap

 local text = nil 
 if mostVotes == nil or mostVoted == nil then
	 chosenMap = game.ReplicatedStorage.Maps:GetChildren()[math.random(1,#game.ReplicatedStorage.Maps:GetChildren())]
 else
	 chosenMap = game.ReplicatedStorage.Maps[mostVoted]		
 end			
  if mostVotes == 1 then
	 text = "Vote"
 else
	text = "Votes"
 end 
 status.Value = chosenMap.Name.." has been selected with "..tostring(mostVotes).." "..text 
 print(chosenMap.Name.." has been selected with "..tostring(mostVotes).." "..text)
 wait(5)

 return chosenMap

end

function module.selectMode()

 local votes = {}
 game.ReplicatedStorage.OtherRemotes.ToggleGameVote:FireAllClients(true)

 for i, v in pairs(game.ReplicatedStorage.Modes:GetChildren()) do
	 votes[v.Name] = {}
 end

 local placeVoteConnection =game.ReplicatedStorage.OtherRemotes.GameVote.OnServerEvent:Connect(function(player,gameMode)    
	if votes[gameMode] then

		for i, playerVotesTab in pairs(votes) do
			for x, playerName in pairs(playerVotesTab) do
				if playerName == player.Name then
					table.remove(playerVotesTab,x)
					break
				end
			end
		end	

		table.insert(votes[gameMode],player.Name)
		game.ReplicatedStorage.OtherRemotes.UpdateVoteCount:FireAllClients(votes)

	end
end)
game.ReplicatedStorage.GameVoting.Value = true
wait(15)
game.ReplicatedStorage.GameVoting.Value = false
game.ReplicatedStorage.OtherRemotes.ToggleGameVote:FireAllClients(false)

placeVoteConnection:Disconnect()

local mostVotes = nil
local mostVoted = nil

for i, modeTable in pairs(votes) do

	local Votes = #modeTable

	if mostVotes == nil then
		mostVotes = Votes
		mostVoted = i

	else

		if Votes >= mostVotes then
			mostVotes = Votes
			mostVoted = i
		end	
	end
end	

local chosenMode

local text = nil 
if mostVotes == nil or mostVoted == nil then
	chosenMode = game.ReplicatedStorage.Modes:GetChildren()[math.random(1,#game.ReplicatedStorage.Modes:GetChildren())]
else
	chosenMode = game.ReplicatedStorage.Modes[mostVoted]		
end			
if mostVotes == 1 then
	text = "Vote"
else
	text = "Votes"
end 
status.Value = chosenMode.Name.." has been selected with "..tostring(mostVotes).." "..text 
print(chosenMode.Name.." has been selected with "..tostring(mostVotes).." "..text)
wait(5)

 return chosenMode
end

return module

Sorry if that was terribly formatted, copy and paste doesnt really format well.

The Script. ServerScriptService.

local roundModule = require(game.ReplicatedStorage.modules:WaitForChild("RoundModule"))
local player = game.Players.LocalPlayer

while wait() do

 roundModule.createIntermission(15)
 local chosenMap = roundModule.selectMap()

 local clonedMap = chosenMap:Clone()
 clonedMap.Name = "Map"
 clonedMap.Parent = workspace

 local players = {}

 for i, v in pairs(game.Players:GetPlayers()) do
	if not v:FindFirstChild("IsBtnClicked") then
		table.insert(players,v)
		print("Added Player "..v.Name)
	end
 end 

 if clonedMap:FindFirstChild("PlayerSpawns") then
	roundModule.teleportPlayers(players, clonedMap:WaitForChild("PlayerSpawns"):GetChildren())
 end

 players = {}

 for i, v in pairs(game.Players:GetPlayers()) do
	if not v:FindFirstChild("IsBtnClicked") then
		table.insert(players,v)
	end
 end 

 if game.Workspace.Lobby:FindFirstChild("Spawns") then
	roundModule.teleportPlayers(players,game.Workspace.Lobby.Spawns:GetChildren())
	players:FindFirstChild("IsBtnClicked").Value = false
 end

 local modeSettings = {
	[game.ReplicatedStorage.Modes.Competitive] = {
		["Time"] = 350;
	};
	[game.ReplicatedStorage.Modes.Randomizer] = {
		["Time"] = 200;
	};
 }

 local chosenMode = roundModule.selectMode()

 if chosenMode == game.ReplicatedStorage.Modes.Randomizer then
	roundModule.StartRound(modeSettings[chosenMode]["Time"], player, clonedMap)
 elseif chosenMode == game.ReplicatedStorage.Modes.Competitive then
	roundModule.StartRound(modeSettings[chosenMode]["Time"], player, clonedMap)
 end

end

Feel free to critisize the script. It also helps in debugging. Thanks.

1 Like