Team not changing for minigame

So I’ve been trying to figure out why it doesn’t change the team from Playing to Not Playing.
I’m not sure what could be happening, any advice?


local settingsModule = require(script:FindFirstChild("Settings"))
local onWin = (function()
	local onWinModule = script:FindFirstChild("OnWin")

	if onWinModule then
		local onWinFunction = require(onWinModule)

		if type(onWinFunction) == "function" then
			return onWinFunction
		end
	end
end)()

local remoteEvent = game:GetService("ReplicatedStorage").Event
local mapsStorage = game:GetService("ServerStorage").Maps:GetChildren()

local playersService = game:GetService("Players")

function minigameModule:isPotentialGame()
	return playersService.NumPlayers >= settingsModule.minimumPlayers
end

function minigameModule:chooseMap()
	local chosenMap = mapsStorage[math.random(1, #mapsStorage)]:Clone()
	chosenMap.Parent = workspace
	return chosenMap
end

local teamService = game:GetService("Teams")
local teamPlaying = teamService.Playing
local teamNotPlaying = teamService["Not Playing  >:("]

function minigameModule:spawnPlayers()
	local playersAlive = self:getAlivePlayers()
	for i,plr in ipairs(playersAlive) do
		if plr.Player.TeamColor == BrickColor.new() then 
			plr.Player.TeamColor = BrickColor.random()
		end 
		plr.Player.Character:MoveTo(Vector3.new(0,0,0)) 
		print(plr.Player.Name .. " set to 'Playing team' and teleported.")
	end
end


function minigameModule:runIntermission()
	if settingsModule.intermissionTime > 0 then
		for currentTime = math.floor(settingsModule.intermissionTime), 0, -1 do
			remoteEvent:FireAllClients("Timer", currentTime)
			wait(1)
		end
	end
	remoteEvent:FireAllClients("CeaseGUIs")
end

function minigameModule:getAlivePlayers()
	local playersAlive = {}
	for _, currentPlayer in pairs(playersService:GetPlayers()) do
		local playerCharacter = currentPlayer.Character
		if playerCharacter then
			local playerHumanoidRoot = playerCharacter:FindFirstChild("HumanoidRootPart")
			local playerHumanoid = playerCharacter:FindFirstChild("Humanoid")
			if playerHumanoid and playerHumanoidRoot then
				table.insert(playersAlive, {
					Player = currentPlayer,
					PlayerHumanoid = playerHumanoid,
					PlayerHumanoidRoot = playerHumanoidRoot
				})
			end
		end
	end
	return playersAlive
end

function minigameModule:isLegalGame()
	if #self:getAlivePlayers() >= settingsModule.minimumPlayers then
		return true
	end
end

function minigameModule:queryGameStart()
	if self.gameRunning then
		return
	elseif self:isPotentialGame() then
		self.gameRunning = true
		remoteEvent:FireAllClients("CeaseGUIs")
		self:runIntermission()

		if self:isLegalGame() then
			if settingsModule.roundDuration > 0 then

				self.currentMap = self:chooseMap()
				local mapWeapons = self.currentMap:FindFirstChild("Weapons")

				local playersAlive = self:getAlivePlayers()
				self.playersAlive = playersAlive

				for index = 1, #playersAlive do
					local currentPlayer = playersAlive[index]
					local backpack = currentPlayer.Player.Backpack

					if backpack and mapWeapons then
						for _, weapon in ipairs(mapWeapons:GetChildren()) do
							weapon:Clone().Parent = backpack
						end
					end

					local connection
					connection = currentPlayer.PlayerHumanoid.Died:Connect(function()
						connection:Disconnect()
						table.remove(playersAlive, index)
						if #playersAlive < 2 then
							local winner = playersAlive[1]
							if winner then
								self:endGame(winner.Player.Name .. " has won!", winner.Player)
							else
								self:endGame("No one has won!")
							end
						end
					end)
				end

				if mapWeapons then
					mapWeapons:Destroy()
				end

				self:spawnPlayers()

				remoteEvent:FireAllClients("Message", self.currentMap.Name .. " was chosen!", 5)

				for currentTime = settingsModule.roundDuration, 0, -1 do
					if not self.gameRunning then
						return
					end
					remoteEvent:FireAllClients("Timer", currentTime)
					wait(1)
				end
				self:endGame("The timer ran out! No one has won!")
			end

		else
			self:endGame("Not enough players alive to begin the round!")
		end
	else
		local remainingPlayers = settingsModule.minimumPlayers - playersService.NumPlayers
		remoteEvent:FireAllClients("Message", "Waiting for " .. remainingPlayers .. " player" .. (remainingPlayers > 1 and "s" or "") .. " to join.")
	end
end

function minigameModule:endGame(outputMessage, winner)
	if self.gameRunning then

		self.gameRunning = false
		self.currentMap:Destroy()
		self.currentMap = nil

		if winner and onWin then
			onWin(winner)
		end

		for _, player in ipairs(playersService:GetPlayers()) do
			player:LoadCharacter()
		end

		wait(1)

		remoteEvent:FireAllClients("Message", outputMessage, 5)

		wait(5)

		self:queryGameStart()
	end
end

function minigameModule:removePlayer(player)
	if self.gameRunning then
		for index, playerData in ipairs(self.playersAlive) do
			if playerData.Player == player then
				table.remove(self.playersAlive, index)
				if #self.playersAlive <= 1 then
					self:endGame("Not enough players to continue the game.")
				end
				break
			end
		end
	end
end

playersService.PlayerAdded:Connect(function()
	minigameModule:queryGameStart()
end)

playersService.PlayerRemoving:Connect(function(player)
	minigameModule:removePlayer(player)
end)

return minigameModule
1 Like