Problems during Round-based system

Hello DevForum, I’m having an issue with my round system not restarting sometimes. I’m not sure why this happens and there are no errors to the dev console. If anyone can help shed some light on the issue, it would be appreciated :slightly_smiling_face:

Main Code
--//Services
local Players = game:GetService('Players');
local Storage = game:GetService('ServerStorage');
local RStorage = game:GetService('ReplicatedStorage');
local General = require(game.ServerScriptService:WaitForChild("Server"):WaitForChild("Modules"):WaitForChild("General"))
local r = Random.new()

--//Variables
local TestMode = game:GetService("RunService"):IsStudio() and true
local PlayerVotesGM = {}
local PlayerVotesMAP = {}
local RoundStarted = RStorage:WaitForChild("RoundStarted")
local MapVoting = RStorage:WaitForChild('Map Voting')
local GamemodeVoting = RStorage:WaitForChild('Gamemode Voting')
local MatchType = RStorage:WaitForChild("MatchType")
local MatchEnded = RStorage:WaitForChild("MatchEnded")
local ChosenMap = RStorage:WaitForChild("ChosenMap")
local TimeToVote = RStorage:WaitForChild("TimeToVote")
local MatchTime = RStorage:WaitForChild("MatchTime")
local InIntermission = RStorage:WaitForChild("InIntermission")
local Intermission = RStorage:WaitForChild('Intermission')
--//Voting Remote
RStorage:WaitForChild("Remotes"):WaitForChild("Events"):WaitForChild("Voting").OnServerEvent:Connect(function(Player,Task,Extra)
	local UserId = tostring(Player.UserId);
	if (not (RoundStarted.Value)) then
		if (Task == "Map Voting") and (MapVoting:FindFirstChild(Extra)) and (MapVoting.Value) then
			warn("Map vote")
			local voteCounts = 1;
			if (game:GetService('MarketplaceService'):UserOwnsGamePassAsync(Player.UserId,9928025)) then
				voteCounts = 2;
			end;
			if (PlayerVotesMAP[UserId]) then
				MapVoting:FindFirstChild(PlayerVotesMAP[UserId]).Value = math.clamp(MapVoting:FindFirstChild(PlayerVotesMAP[UserId]).Value-voteCounts,0,200);
			end;
			PlayerVotesMAP[UserId] = Extra;
			MapVoting:FindFirstChild(Extra).Value = MapVoting:FindFirstChild(Extra).Value + voteCounts;
			warn(Player.Name .. " Voted for " .. MapVoting:FindFirstChild(Extra).Name)
			warn(MapVoting:FindFirstChild(Extra).Name .. " now has " .. MapVoting:FindFirstChild(Extra).Value .. " votes")
		elseif Task == "Gamemode Voting" and GamemodeVoting:FindFirstChild(Extra) and (GamemodeVoting.Value) then
			warn"Gamemode vote"
			local voteCounts = 1;
			if (game:GetService('MarketplaceService'):UserOwnsGamePassAsync(Player.UserId,9928036)) then
				voteCounts = 2;
			end;
			if (PlayerVotesGM[UserId]) then
				GamemodeVoting:FindFirstChild(PlayerVotesGM[UserId]).Value = math.clamp(GamemodeVoting:FindFirstChild(PlayerVotesGM[UserId]).Value-voteCounts,0,200);
			end;
			PlayerVotesGM[UserId] = Extra;
			GamemodeVoting:FindFirstChild(Extra).Value = GamemodeVoting:FindFirstChild(Extra).Value + voteCounts;
			warn(Player.Name .. " Voted for " .. GamemodeVoting:FindFirstChild(Extra).Name)
			warn(GamemodeVoting:FindFirstChild(Extra).Name .. " now has " .. GamemodeVoting:FindFirstChild(Extra).Value .. " votes")
		end;
	end;
end);

--//Wait For Players
while true do
	repeat wait() until #RStorage.ReadyPlayers:GetChildren() > 1
	warn'ready'
	
	--//Reset game
	local function Reset()
		if workspace:FindFirstChild(ChosenMap.Value) then
			workspace[ChosenMap.Value]:Destroy()
		end
		for i, v in pairs(game.Players:GetPlayers()) do
			if v:FindFirstChild("leaderstats") then
				v.leaderstats.Kills.Value = 0
				v.leaderstats.Deaths.Value = 0
				v.leaderstats.Points.Value = 0
			end
			General.Stun(v.Character)
			v.KunaiAmount.Value = 8
		end
		ChosenMap.Value = "-"
		GamemodeVoting.Value = false
		MapVoting.Value = false
		MatchEnded.Value = false
		MatchTime.Value = 0
		MatchType.Value = "-"
		RoundStarted.Value = false
		TimeToVote.Value = 0
		Intermission.Value = TestMode and 3 or 20;
		warn'reset game'
	end
	pcall(Reset)
	--game:BindToClose(Reset)
	
	--//Intermission
	InIntermission.Value = true
	RStorage.Remotes.Events.Effects2:FireAllClients("Lobby")
	warn'intermission'
	--Intermission.Value = 20
	repeat Intermission.Value = Intermission.Value - 1; wait(1); until Intermission.Value <= 0
	InIntermission.Value = false
	
	--//Choose Gamemode
	for _,vote in pairs(GamemodeVoting:GetChildren()) do
		vote.Value = 0;
	end;
	GamemodeVoting.Value = true;
	game.ReplicatedStorage.TimeToVote.Value = TestMode and 3 or 10;
	repeat game.ReplicatedStorage.TimeToVote.Value = game.ReplicatedStorage.TimeToVote.Value - 1;wait(1); until game.ReplicatedStorage.TimeToVote.Value <= 0;
	GamemodeVoting.Value = false;
	local chosenMap = GamemodeVoting["Free-For-All"]
	for _,vote in pairs(GamemodeVoting:GetChildren()) do
		if (chosenMap) then
			if (chosenMap.Value <= vote.Value) then
				chosenMap = vote;
			end;
		end;
	end;
	MatchType.Value = chosenMap.Name
	warn("Chosen gamemode: " .. chosenMap.Name)
	
	--//Randomize 3 maps to show
	local randomMOD = require(game.ServerStorage:WaitForChild("random"))
	local section = r:NextInteger(1, #randomMOD)
	
	--//Choose Map
	for _,vote in pairs(MapVoting:GetChildren()) do
		vote.Value = 0;
	end;
	MapVoting.Value = true;
	for _, v in pairs(RStorage.ReadyPlayers:GetChildren()) do
		RStorage["Display Maps"]:FireClient(v.Value, unpack(randomMOD[section]))
	end
	game.ReplicatedStorage.TimeToVote.Value = TestMode and 3 or 10;
	repeat game.ReplicatedStorage.TimeToVote.Value = game.ReplicatedStorage.TimeToVote.Value - 1;wait(1); until game.ReplicatedStorage.TimeToVote.Value <= 0;
	MapVoting.Value = false;
	local chosenMap2 = MapVoting:GetChildren()[math.random(1,#MapVoting:GetChildren())];
	for _,vote in pairs(MapVoting:GetChildren()) do
		if (chosenMap2) then
			if (chosenMap2.Value <= vote.Value) then
				chosenMap2 = vote;
			end;
		end;
	end;
	ChosenMap.Value = chosenMap2.Name
	warn("Chosen map: " .. chosenMap2.Name)
	RStorage.Remotes.Events.Effects2:FireAllClients("".. chosenMap2.Name)
	--//Run Gamemode
	local Module = script:FindFirstChild(MatchType.Value) or script["Free-For-All"];
	PlayerVotesGM = {}
	PlayerVotesMAP = {}
	warn("Enabled " .. Module.Name)
	local RoundEnd = require(Module)();
	local s = tick()
	repeat wait() until RoundEnd or tick() - s >= MatchTime.Value + 5 --[[failsafe]];
end
Round code
return function()
	warn"Free for all initated"
	local RStorage = game:GetService('ReplicatedStorage')
	local SStorage = game:GetService("ServerStorage")
	local Players = game:GetService("Players")
	
	local General = require(script.Parent.Parent.Server.Modules.General)
	
	local RoundStarted = RStorage:WaitForChild("RoundStarted")
	local MapVoting = RStorage:WaitForChild('Map Voting')
	local GamemodeVoting = RStorage:WaitForChild('Gamemode Voting')
	local MatchType = RStorage:WaitForChild("MatchType")
	local MatchEnded = RStorage:WaitForChild("MatchEnded")
	local ChosenMap = RStorage:WaitForChild("ChosenMap")
	local MatchTime = RStorage:WaitForChild("MatchTime")
	local ReadyPlayers = RStorage:WaitForChild("ReadyPlayers")
	local map
	if ChosenMap.Value then
	map = SStorage.Maps[ChosenMap.Value]:Clone()
	else
		map = SStorage.Maps:GetChildren()[math.random(1, #SStorage.Maps:GetChildren())]:Clone()
	end
	map.Parent = workspace
	wait(1)
	for i, Player in pairs(ReadyPlayers:GetChildren()) do
		Player = Player.Value
		local Character = Player.Character or Player.CharacterAdded:Wait()
		if map:FindFirstChild("SPAWNS") then
			if Character then
				local s, e =  pcall(function()
					Character:SetPrimaryPartCFrame(map.SPAWNS:GetChildren()[i].CFrame * CFrame.new(0,5,0))
				end)
				if not s then warn('Teleport error: ' .. e) end
			end		
		end
		General.Unstun(Character)
		warn'transporting players'
	end
	
	RoundStarted.Value = true
	warn'free for all started'
	
	MatchTime.Value = 60*10
	repeat 
		MatchTime.Value = MatchTime.Value - 1;
		wait(1); 
	until MatchTime.Value <= 0;
	
	RoundStarted.Value = false;
	
	local winner;
	for _,v in pairs(ReadyPlayers:GetChildren()) do	
		local Player = v.Value
		if (winner) then
			if (Player.leaderstats.Points.Value >= winner.leaderstats.Points.Value) then
				winner = Player;
			end;
		else
			winner = Player;
		end
	end;
	
	for _,v in pairs(ReadyPlayers:GetChildren()) do	
		spawn(function()
			local Player = v.Value
			General.Stun(Player.Character)
			Player.Character.Humanoid.WalkSpeed = 0
			Player.Character.Humanoid.JumpPower = 0
			local endInfo = Instance.new("BoolValue",game.ReplicatedStorage.EndInfo);
			endInfo.Name = Player.Name;
			if (Player == winner) then
				endInfo.Value = true;
			end;
			game.ReplicatedStorage.Remotes.Events["Display Winner"]:FireClient(Player, endInfo.Value)
			if winner.Character then
				warn'tweened'
				game.ReplicatedStorage.Remotes.Events["TweenCamera"]:FireClient(Player, 'Tween', 1, CFrame.new((winner.Character.HumanoidRootPart.CFrame * CFrame.new(0,2,-5)).p, winner.Character.HumanoidRootPart.Position))
			end
		end);
	end;
	RStorage.EndInfo:ClearAllChildren()
	wait(3)
	
	--//Reset players
	for _, v in pairs(ReadyPlayers:GetChildren()) do
		game.ReplicatedStorage.Remotes.Events["TweenCamera"]:FireClient(v.Value, "Reset")
		if v.Value.Character then
			v.Value.Character.Humanoid:TakeDamage(math.huge)
			warn('killed ' .. v.Value.Name)
		end
	end
	--readyconn:Disconnect()
	wait(1)
	warn'match end'
	MatchEnded.Value = true
	return true
end
1 Like

Why do you have a pcall on reset?

To make sure it doesn’t error and stop the script.