Game only starts once

Hey I am currently making a game about a game-mode based on fall guys and its all going good, yet when I play the game the game starts as normal, when it ends it refreshes the map but then constantly says it is still waiting for players even if there is enough players in the server for it.

There are no errors and haven’t started to attempt to de-bug it.

Code:

local Players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local mps = game:GetService("MarketplaceService")
local config = require(script:WaitForChild("Configuration"))

local event = rs:WaitForChild("Events"):WaitForChild("UpdateText")

local queuedPlayers = {}
local activePlayers = {}

local gameActive = script:WaitForChild("GameActive")
local countingdown = script:WaitForChild("CountingDown")

local spawns = workspace:WaitForChild("Spawns")

local function updateText(message, visible)
	event:FireAllClients(message, visible)
end

local function queuePlayers()
	if gameActive.Value == false then
		for i, player in pairs(Players:GetPlayers()) do
			if not table.find(queuedPlayers, player.UserId) then
				table.insert(queuedPlayers, player.UserId)
			end
		end
	end
end

Players.PlayerAdded:Connect(queuePlayers)

local function removePlayer(player)
	local id = player.UserId
	
	table.remove(queuedPlayers, table.find(queuedPlayers, id))
	table.remove(activePlayers, table.find(activePlayers, id))
end

local function checkForPlayers()
	while task.wait(1) do
		if gameActive == true or countingdown.Value == true then continue end
		
		queuePlayers()
		
		if #queuedPlayers >= config.MinPlayers then
			countingdown.Value = true
			print("Countdown started")
		else
			updateText("Waiting for Players...", true)
		end
	end
end

local function gameStart()
	local playersSetup = {}
	while task.wait(1) do
		for i, player in pairs(Players:GetPlayers()) do
			local userId = player.UserId
			
			if table.find(queuedPlayers, userId) and not playersSetup[userId] then
				table.remove(queuedPlayers, table.find(queuedPlayers, userId))
				table.insert(activePlayers, userId)
				playersSetup[userId] = true
				
				local character = player.Character
				local humanoid = character.Humanoid
				local humanoidRootPart = character.HumanoidRootPart
				local spawnNum = math.random(1, #spawns:GetChildren())
				local spawnPart = spawns[spawnNum]
				
				humanoidRootPart.CFrame = spawnPart.CFrame + Vector3.new(0, 5, 0)
				
				local diedConnection
				
				diedConnection = humanoid.Died:Connect(function()
					removePlayer(player)
					diedConnection:Disconnect()
				end)
			end
		end
		
		if #activePlayers == 1 then
			local winner = Players:GetPlayerByUserId(activePlayers[1])
			print(winner.Name, " has won")
			updateText(winner.Name.. " has won the game!", true)
			
			task.wait(5)
			
			winner.Character.HumanoidRootPart.CFrame = workspace.SpawnLocation.CFrame + Vector3.new(0, 5, 0)
			winner.leaderstats["🏆Wins"].Value += 1
			winner.leaderstats["💰Cash"].Value += 10
			
			if mps:UserOwnsGamePassAsync(winner.UserId, 783195175) then
				winner.leaderstats["🏆Wins"].Value += 1

			elseif mps:UserOwnsGamePassAsync(winner.UserId, 783258017) then
				winner.leaderstats["💰Cash"].Value += 10

			elseif mps:UserOwnsGamePassAsync(winner.UserId, 782995690) then
				winner.leaderstats["💰Cash"].Value += 15
			
				gameActive.Value = false
			end
				
				workspace.Blocks:Destroy()
				local blockClone = rs.Blocks:Clone()
				blockClone.Parent = workspace
				checkForPlayers()

			continue
		end
	end
	
	task.wait(1)
	checkForPlayers()
end

countingdown.Changed:Connect(function()
	if countingdown.Value == true then
		for i = config.countDown, 0, -1 do
			if #queuedPlayers < config.MinPlayers then
				countingdown.Value = false
				checkForPlayers()
				return
			end
			
			task.wait(1)
			updateText("Starting in ".. i.." seconds!", true)
		end
		
		if #queuedPlayers < config.MinPlayers then
			countingdown.Value = false
			task.wait(1)
			checkForPlayers()
			return
		end	
		
		countingdown.Value = false
		gameActive.Value = true
		updateText("", false)
		print("Countdown ended")
		gameStart()
	end
end)

checkForPlayers()

Players.PlayerRemoving:Connect(removePlayer)
2 Likes

Have you tried printing some of your variables where you think it’s erroring? Like printing the two tables to actually make sure everything’s getting properly added.

it doesnt error cause it works… it just runs once rather then looping