Game Not Ending Correctly

this is now the THIRD problem i’ve had with this game with no clue how to solve. here i am, once more turning to your help.

here’s the issue: i’m trying to tell if remaining players are on the same team, and if so? the game is ended and the people on that team are winners. if it’s a 1v1 on seperate teams and one player wins, the game isn’t ending!

here’s the entire main script:

-- Services --

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")

-- Modules --

local RoundModule = require(script.RoundModule)

-- Guis --

local StatusGui = StarterGui:WaitForChild("StatusGui")

-- Variables --

local IntermissionTime = 15
local VotingTime = 10
local RoundTime
local AbleToWin = true

local StatusValue = ReplicatedStorage:WaitForChild("Values"):FindFirstChild("StatusValue")

-- Folders --

local Minigames = ReplicatedStorage:WaitForChild("Minigames"):GetChildren()
local CurrentMinigame = game:GetService("Workspace"):WaitForChild("CurrentMinigame")
local InGameFolder = game:GetService("Workspace"):WaitForChild("InGameFolder")

-- Tables --

local WinnersTable = {}

-- Script --

while true do
	
	IntermissionTime = 15
	
	repeat
	IntermissionTime -= 1
	
	StatusValue.Value = "Intermission: "..IntermissionTime
	task.wait(1)
	until IntermissionTime == 0
	
	print("Starting round.")
	StatusValue.Value = "Round starting..."
	task.wait(1)
	StatusValue.Value = "Round starting.."
	task.wait(1)
	StatusValue.Value = "Round starting."
	task.wait(1)
	StatusValue.Value = "Round starting"

	local RandomMinigame = math.random(1, #Minigames)
	local ChosenMinigame = Minigames[RandomMinigame]:Clone()
	ChosenMinigame.Parent = CurrentMinigame

	local Players = game:GetService("Players"):GetChildren()
	
	for i = 1, #Players do
		if Players[i].Character ~= nil then
			if ChosenMinigame:FindFirstChild("SwordFight") then
				
				-- Add Teams
				
				local Teams = game:GetService("Teams")
				
				local BlueTeam = Instance.new("Team", Teams)
				BlueTeam.Name = "Blue"
				BlueTeam.TeamColor = BrickColor.new("Bright blue")
				
				local RedTeam = Instance.new("Team", Teams)
				RedTeam.Name = "Red"
				RedTeam.TeamColor = BrickColor.new("Really red")
				
				local RandomNum = math.random(1, 2)
				local SelectedTeam
				
				if RandomNum == 1 then
					SelectedTeam = "Blue"
				else
					SelectedTeam = "Red"
				end
				
				Players[i].Team = Teams:FindFirstChild(SelectedTeam)
				
				-- Spawn Player.
				
				if Players[i].Team == "Blue" then
					Players[i].Character.Head.CFrame = ChosenMinigame.BlueBase.BlueSpawn.CFrame
				else
					Players[i].Character.Head.CFrame = ChosenMinigame.RedBase.RedSpawn.CFrame
				end
				
				local Sword = game:GetService("ServerStorage"):FindFirstChild("ClassicSword"):Clone()
				Sword.Parent = Players[i].Backpack
				
				Players[i].Character.Parent = InGameFolder
				
			else
				
				-- Normal Spawn.

				local RandomTeleportPoint = math.random(1, #ChosenMinigame:WaitForChild("TeleportPoints"):GetChildren())
				Players[i].Character.Head.CFrame = ChosenMinigame.TeleportPoints[RandomTeleportPoint].CFrame
				Players[i].Character.Parent = InGameFolder
				
			end
		end
	end
	
	AbleToWin = true
	
	if ChosenMinigame:FindFirstChild("ObstacleRace") then
		-- Set up Minigame.
		RoundTime = 10
		
		-- Minigame Over.
		
		ChosenMinigame:WaitForChild("EndPoint").Touched:Connect(function(Hit)
			if Hit.Parent:FindFirstChild("Humanoid") then
				if AbleToWin == true then
					AbleToWin = false
					local Winner = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
					table.insert(WinnersTable, Winner.Name)
					print("Touched.")
				end
			end
		end)
	end
	
	if ChosenMinigame:FindFirstChild("Maze") then
		-- Set up Minigame.
		RoundTime = 60
		RoundModule.LockFirstPerson()
		game:GetService("Lighting").ClockTime = 0
		
		-- Minigame Over.
		
		ChosenMinigame:WaitForChild("EndPoint").Touched:Connect(function(Hit)
			if Hit.Parent:FindFirstChild("Humanoid") then
				if AbleToWin == true then
					local Winner = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
					if table.find(WinnersTable, Winner.Name) then
						print("Already won.")
					else
						table.insert(WinnersTable, Winner.Name)
						print("Touched.")	
						Winner.Character:MoveTo(ChosenMinigame:FindFirstChild("WinnerTeleport").Position)
						Winner.Character.Parent = game:GetService("Workspace")
					end
				end
			end
		end)
	end
	
	if ChosenMinigame:FindFirstChild("SwordFight") then
		--  Set up Minigame.
		RoundTime = 60
		AbleToWin = true
		
		while task.wait() do
			local TeamColor1 = game.Players:GetPlayers()[1].TeamColor
			local IsSameTeam = true
			
			for i, Player in pairs(game:GetService("Players"):GetPlayers()) do
				if Player.Character.Parent == InGameFolder then
					if i == 1 then
							TeamColor1 = Player.TeamColor
						end
						if Player.TeamColor ~= TeamColor1 then
							IsSameTeam = false
							break
						end
				end
			end
			
			if IsSameTeam then 
				AbleToWin = false
				
				for i, Player in pairs(InGameFolder:GetChildren()) do
					if table.find(WinnersTable, Player.Name) then
						-- Skip.
					else
						table.insert(WinnersTable, Player.Name)
					end
				end
			end
		end
		
	end
	
	repeat
		RoundTime -= 1
		StatusValue.Value = "Round over in: "..RoundTime
		task.wait(1)
	until RoundTime == 0 or AbleToWin == false or #InGameFolder:GetChildren() == 0
	
	AbleToWin = false
	RoundModule.UnlockFirstPerson()
	game:GetService("Lighting").ClockTime = 14.5
	StatusValue.Value = "Round over!"
	
	task.wait(3)
	StatusValue.Value = "Winners: "..table.concat(WinnersTable)
	
	for i, Player in pairs(game:GetService("Players"):GetPlayers()) do
		if table.find(WinnersTable, Player.Name) then
			Player:WaitForChild("leaderstats").Wins.Value += 1
		end
	end
	
	RoundModule.NewRound()
	WinnersTable = {}
	
	task.wait(3)
end

here’s the most crucial points including this problem:

local RandomMinigame = math.random(1, #Minigames)
	local ChosenMinigame = Minigames[RandomMinigame]:Clone()
	ChosenMinigame.Parent = CurrentMinigame

	local Players = game:GetService("Players"):GetChildren()
	
	for i = 1, #Players do
		if Players[i].Character ~= nil then
			if ChosenMinigame:FindFirstChild("SwordFight") then
				
				-- Add Teams
				
				local Teams = game:GetService("Teams")
				
				local BlueTeam = Instance.new("Team", Teams)
				BlueTeam.Name = "Blue"
				BlueTeam.TeamColor = BrickColor.new("Bright blue")
				
				local RedTeam = Instance.new("Team", Teams)
				RedTeam.Name = "Red"
				RedTeam.TeamColor = BrickColor.new("Really red")
				
				local RandomNum = math.random(1, 2)
				local SelectedTeam
				
				if RandomNum == 1 then
					SelectedTeam = "Blue"
				else
					SelectedTeam = "Red"
				end
				
				Players[i].Team = Teams:FindFirstChild(SelectedTeam)
				
				-- Spawn Player.
				
				if Players[i].Team == "Blue" then
					Players[i].Character.Head.CFrame = ChosenMinigame.BlueBase.BlueSpawn.CFrame
				else
					Players[i].Character.Head.CFrame = ChosenMinigame.RedBase.RedSpawn.CFrame
				end
				
				local Sword = game:GetService("ServerStorage"):FindFirstChild("ClassicSword"):Clone()
				Sword.Parent = Players[i].Backpack
				
				Players[i].Character.Parent = InGameFolder
				
			else
				
				-- Normal Spawn.

				local RandomTeleportPoint = math.random(1, #ChosenMinigame:WaitForChild("TeleportPoints"):GetChildren())
				Players[i].Character.Head.CFrame = ChosenMinigame.TeleportPoints[RandomTeleportPoint].CFrame
				Players[i].Character.Parent = InGameFolder
				
			end
		end
	end

if ChosenMinigame:FindFirstChild("SwordFight") then
		--  Set up Minigame.
		RoundTime = 60
		AbleToWin = true
		
		while task.wait() do
			local TeamColor1 = game.Players:GetPlayers()[1].TeamColor
			local IsSameTeam = true
			
			for i, Player in pairs(game:GetService("Players"):GetPlayers()) do
				if Player.Character.Parent == InGameFolder then
					if i == 1 then
							TeamColor1 = Player.TeamColor
						end
						if Player.TeamColor ~= TeamColor1 then
							IsSameTeam = false
							break
						end
				end
			end
			
			if IsSameTeam then 
				AbleToWin = false
				
				for i, Player in pairs(InGameFolder:GetChildren()) do
					if table.find(WinnersTable, Player.Name) then
						-- Skip.
					else
						table.insert(WinnersTable, Player.Name)
					end
				end
			end
		end
		
	end
	
	repeat
		RoundTime -= 1
		StatusValue.Value = "Round over in: "..RoundTime
		task.wait(1)
	until RoundTime == 0 or AbleToWin == false or #InGameFolder:GetChildren() == 0
	
	AbleToWin = false
	RoundModule.UnlockFirstPerson()
	game:GetService("Lighting").ClockTime = 14.5
	StatusValue.Value = "Round over!"
	
	task.wait(3)
	StatusValue.Value = "Winners: "..table.concat(WinnersTable)
	
	for i, Player in pairs(game:GetService("Players"):GetPlayers()) do
		if table.find(WinnersTable, Player.Name) then
			Player:WaitForChild("leaderstats").Wins.Value += 1
		end
	end
	
	RoundModule.NewRound()
	WinnersTable = {}
	
	task.wait(3)
end

any help on this would be highly appreciated! :heavy_heart_exclamation::heavy_heart_exclamation:

i’m not really sure what’s going on with this code, so i’m going to attempt to clean it up before i help

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local Teams = game:GetService("Teams")

local RoundModule = require(script:WaitForChild("RoundModule"))
local StatusGui = StarterGui:WaitForChild("StatusGui")

local IntermissionTime = 15
local VotingTime = 10
local AbleToWin = true
local RoundTime

local StatusValue = ReplicatedStorage:WaitForChild("Values"):FindFirstChild("StatusValue")

local Minigames = ReplicatedStorage:WaitForChild("Minigames"):GetChildren()
local CurrentMinigame = workspace:WaitForChild("CurrentMinigame")
local InGameFolder = workspace:WaitForChild("InGameFolder")

local Winners = {}

local BlueTeam = Instance.new("Team", Teams)
BlueTeam.Name = "Blue"
BlueTeam.TeamColor = BrickColor.new("Bright blue")

local RedTeam = Instance.new("Team", Teams)
RedTeam.Name = "Red"
RedTeam.TeamColor = BrickColor.new("Really red")

while true do
  IntermissionTime = 15

  repeat
    IntermissionTime -= 1
    StatusValue.Value = "Intermission: ".. IntermissionTime
    wait(1)
  until IntermissionTime == 0

  print("Starting round.")
  StatusValue.Value = "Round starting..."
  
  for i = 1, 3 do 
    StatusValue.Value = StatusValue.Value:sub(0, -2)
    wait(1)
  end

  local RandomMinigame = Minigames[math.random(#Minigames)]:Clone()
  RandomMinigame.Parent = CurrentMinigame

  for _, Player in Players:GetPlayers() do
    if not Player.Character then continue end
    if RandomMinigame:FindFirstChild("SwordFight") then
      local SelectedTeam = math.random(1) == 0 and "Blue" or "Red"

      Player.Team = Teams:FindFirstChild(SelectedTeam)
      Player.Character.Head.CFrame = SelectedTeam == "Blue" and RandomMinigame.BlueBase.BlueSpawn.CFrame or RandomMinigame.RedBase.RedSpawn.CFrame

      local Sword = ServerStorage:FindFirstChild("ClassicSword"):Clone()
      Sword.Parent = Player.Backpack

      Player.Character.Parent = InGameFolder
      continue
    end
    
    local TeleportPoints = RandomMinigame:FindFirstChild("TeleportPoints")
    local RandomTeleportPoint = TeleportPoints[math.random(#TeleportPoints:GetChildren())]
    Player.Character.Head.CFrame = RandomTeleportPoint.CFrame
    Player.Character.Parent = InGameFolder
  end

  AbleToWin = true

  if RandomMinigame:FindFirstChild("ObstacleRace") then
    RoundTime = 10
    
    local Connection
    
    Connection = RandomMinigame:WaitForChild("EndPoint").Touched:Connect(function(Hit)
      if not Hit.Parent:FindFirstChild("Humanoid") or not AbleToWin then return end
      AbleToWin = false
      local Winner = Players:GetPlayerFromCharacter(Hit.Parent)
      Winners[#Winners + 1] = Winner
      print("Touched.")
      Connection:Disconnect()
    end)
  end

  if RandomMinigame:FindFirstChild("Maze") then
    RoundTime = 60
    RoundModule.LockFirstPerson()
    Lighting.ClockTime = 0
    
    local Connection

    Connection = RandomMinigame:WaitForChild("EndPoint").Touched:Connect(function(Hit)
      if not Hit.Parent:FindFirstChild("Humanoid") or not AbleToWin then return end
      local Winner = Players:GetPlayerFromCharacter(Hit.Parent)
      if table.find(Winners, Winner) then print("Already won.") return end

      Winners[#Winners + 1] = Winner
      Winner.Character:MoveTo(RandomMinigame:FindFirstChild("WinnerTeleport").Position)
      Winner.Character.Parent = workspace
      
      print("Touched.")	
      Connection:Disconnect()
    end)
  end

  if RandomMinigame:FindFirstChild("SwordFight") then
    --  Set up Minigame.
    RoundTime = 60
    AbleToWin = true

    while task.wait(1) do
      local TeamColor1 = Players:GetPlayers()[1].TeamColor
      local IsSameTeam = true

      for i, Player in game:GetService("Players"):GetPlayers() do
        if Player.Character.Parent ~= InGameFolder then continue end
        if i == 1 then
          TeamColor1 = Player.TeamColor
        end
        if Player.TeamColor ~= TeamColor1 then -- This check would always be false for the first Player
          IsSameTeam = false
          break
        end
      end

      if IsSameTeam then 
        AbleToWin = false

        for _, Player in InGameFolder:GetChildren() do
          if not table.find(Winners, Player.Name) then continue end
          Winners[#Winners + 1] = Player.Name
        end
      end
    end

  end

  repeat
    RoundTime -= 1
    StatusValue.Value = "Round over in: ".. RoundTime
    wait(1)
  until RoundTime == 0 or AbleToWin == false or #InGameFolder:GetChildren() == 0

  AbleToWin = false
  RoundModule.UnlockFirstPerson()
  Lighting.ClockTime = 14.5
  StatusValue.Value = "Round over!"

  wait(3)
  
  StatusValue.Value = "Winners: ".. table.concat(Winners)

  for _, Player in Players:GetPlayers() do
    if not table.find(Winners, Player) then continue end
    Player:WaitForChild("leaderstats").Wins.Value += 1
  end

  RoundModule.NewRound()
  Winners = {}

  wait(3)
end

not really sure if this is the best way to handle minigames but i would just put them into a table inside a module

local MinigameHandler = {}

local Minigames = {
  ["ObstacleRace"] = {
    ["Duration"] = 0;
    ["Play"] = function()
      -- Setup and play this minigame
    end;
  };
  
  ["Maze"] = {
    ["Duration"] = 0;
    ["Play"] = function()
      -- Setup and play this minigam
    end;
  };
  
  ["SwordFight"] = {
    ["Duration"] = 60;
    ["Name"] = "SwordFight";
    ["Play"] = function(self)
      MinigameHandler.PutPlayersInRandomTeams()
      
      local Connection = MinigameHandler.PlayerDied:Connect(function(Player, LeftGame)
        local PlayerIndex = table.find(PlayersInGame, Player)
        if not PlayerIndex then return end
        PlayersInGame[PlayerIndex] = nil
        
        if LeftGame then return end
        MinigameHandler.SetDefaultTeam(Player)
      end)
      
      repeat wait(1) until #PlayersInGame == 1 or CurrentMinigame ~= self.Name
      Winners = PlayersInGame
      Connection:Disconnect(); MinigameHandler.GameEnded:Fire()
    end;
  };
  
  ["ObstacleRace"] = {
    ["Duration"] = 0;
    ["Play"] = function()
      -- Setup and play this minigame
    end;
  };
}

return MinigameHandler
1 Like

Your code is hard to read, have you thought of using remote events to call other scripts, so you don’t cram it all inside of one script?

2 Likes

i’m aware that my codes a little messy, i’m still basically new to scripting.
i use a module script to try and organize a little more. also sorry, i did end up solving this issue on my own, thanks.