Round Winner system bugged

Hello,
This is my first topic about problem. Actually my friend helped me to create a round system with winner system so issue is ( winner system is bugged ) whenever round is ending its saying player win the round but all players are dead it must declare no winners but its not happening help me to solve it please.


function winnering()

	for _, Player in pairs(Players:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then


			table.insert(playersInRound, Player) -- add all players to table
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
			end)
		end
	end
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	
	if #playersInRound < 1 then
		local success, message = pcall(function()
			local statusText = "There's Is No Winner But Well Played"
			for i, player in pairs(playersInRound) do
				-- Concat the Status text with the player's name
				if i ~= #playersInRound then
					-- Add a comma if it is not the last player(winner)
					-- You want (player, player, player) not (player, player, player, )
					statusText = statusText ..  ", " 
				end
				-- Your stats code

				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 150
			end
			print(statusText)
			game.Workspace.Statement.Value = statusText
		end)
		if not success then
			warn("An error occurred while rewarding winner: " .. message)
		end
		

	

	elseif #playersInRound > 1 then --- Check if there is more than one winner
		local success, message = pcall(function() --- I just used your pcall example
			local statusText = "The Winners are: " -- Creating a base message for the Status
			for i, player in pairs(playersInRound) do -- Loop through all winners
				statusText = statusText .. player.Name -- Concat the Status text with the player's name
				if i ~= #playersInRound then
					-- Add a comma if it is not the last player(winner)
					-- You want (player, player, player) not (player, player, player, )
					statusText = statusText ..  ", " 
				end
				-- Your stats code
				player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 300
			end
			print(statusText)
			game.Workspace.Statement.Value = statusText
		end)
		if not success then
			warn("An error occurred while rewarding winner: " .. message)
		end

	elseif #playersInRound == 1 then
		local success, message = pcall(function()
			local player = playersInRound[1]
			print("The Winner is: " .. player.Name)
			game.Workspace.Statement.Value = player.Name .. " won the game!"
			player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
			player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 300
			wait(3)
		end)
		
		if not success then
			warn("An error occurred while rewarding winner: " .. message)
			end
		end
	end


	
while true do




	--teleport the players

while wait(0) do
	map = game
	randomMap = ""
	
	game.Workspace.Statement.Value = '4 players needed to start a game'

	repeat 
		wait(1)
	until #game.Players:GetPlayers() >= 1
	


	for i = defaultIntermission,1,-1 do
		game.Workspace.Statement.Value = tostring("Intermission: " ..i.. " seconds")
		wait(1)
	end

	randomMap = mapModule[math.random(1,#mapModule)]
	game.Workspace.Statement.Value = "The map we will be playing is " ..randomMap

	map = game.Lighting.Maps:FindFirstChild(randomMap)
	map.Parent = game.Workspace.MapWorkspace

	wait(3)
	game.Workspace.Statement.Value = "Teleporting Players..."
	wait(3)


	local totalSpawns = 0

	for i,v in pairs (map.Spawns:GetChildren()) do
		if v:IsA("Part") then
			totalSpawns = totalSpawns + 1
		end
	end

	local randomSpawn = 0

	for i,v in pairs (game.Players:GetChildren()) do
		if v:IsA("Player") then
			randomSpawn = math.random(1,totalSpawns)
			for i,v in pairs (game.Players:GetChildren()) do
				if v:IsA("Player") then
					randomSpawn = math.random(1,totalSpawns)
					game.Workspace[v.Name]:MoveTo(map.Spawns:GetChildren()[randomSpawn].Position + Vector3.new(0,5,0))
				end
			end
		end
	end

	for i = tonumber(map.Length.Value), 1, -1 do

		game.Workspace.Statement.Value = "Time Left: " ..i.. " seconds"
		wait(1)
	end

	game.Workspace.Statement.Value = "Map Over"
    winnering()
		
	wait(10)
		
	onClicked()
		
	for i,v in pairs(game.Players:GetPlayers()) do
			if v:IsA("Player") then

			game.Workspace[v.Name]:MoveTo(game.Workspace.SpawnLocation.Position + Vector3.new(0,5,0))
		end
	end
		task.wait(5)

		map.Parent = game.Lighting.Maps
		end
end

You need to call “winnering” whenever a player dies/leaves the game.

Can Help me to execute because my friend is off since couple of weeks and i am not too experience this is the all script codes

Try adding this at the bottom of the script with the “winnering” function.

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)

local h = char:WaitForChild("Humanoid")
h.Died:Connect(winnering)

end)
end)
game.Players.PlayerRemoving:Connect(winnering)

This is the whole script if you feel confusion

local defaultIntermission = 30
local mapModule = require(game.ServerScriptService.MapModule)
local randomMap = ""
local map = game

local Players = game:GetService("Players")
local playersInRound = {} -- track alive players
local connections = {} -- store connections to disconnect later

s = game:service("TeleportService")
id = 6280789449 --Game ID Here

function onClicked()
	s:Teleport(6280789449)
end

-------------------fgdgdfg



function winnering()

	for _, Player in pairs(Players:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then


			table.insert(playersInRound, Player) -- add all players to table
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
			end)
		end
	end
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	
	if #playersInRound < 1 then
		local success, message = pcall(function()
			local statusText = "There's Is No Winner But Well Played"
			for i, player in pairs(playersInRound) do
				-- Concat the Status text with the player's name
				if i ~= #playersInRound then
					-- Add a comma if it is not the last player(winner)
					-- You want (player, player, player) not (player, player, player, )
					statusText = statusText ..  ", " 
				end
				-- Your stats code

				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 150
			end
			print(statusText)
			game.Workspace.Statement.Value = statusText
		end)
		if not success then
			warn("An error occurred while rewarding winner: " .. message)
		end
		

	

	elseif #playersInRound > 1 then --- Check if there is more than one winner
		local success, message = pcall(function() --- I just used your pcall example
			local statusText = "The Winners are: " -- Creating a base message for the Status
			for i, player in pairs(playersInRound) do -- Loop through all winners
				statusText = statusText .. player.Name -- Concat the Status text with the player's name
				if i ~= #playersInRound then
					-- Add a comma if it is not the last player(winner)
					-- You want (player, player, player) not (player, player, player, )
					statusText = statusText ..  ", " 
				end
				-- Your stats code
				player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 300
			end
			print(statusText)
			game.Workspace.Statement.Value = statusText
		end)
		if not success then
			warn("An error occurred while rewarding winner: " .. message)
		end

	elseif #playersInRound == 1 then
		local success, message = pcall(function()
			local player = playersInRound[1]
			print("The Winner is: " .. player.Name)
			game.Workspace.Statement.Value = player.Name .. " won the game!"
			player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
			player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 300
			wait(3)
		end)
		
		if not success then
			warn("An error occurred while rewarding winner: " .. message)
			end
		end
	end


	
while true do




	--teleport the players

while wait(0) do
	map = game
	randomMap = ""
	
	game.Workspace.Statement.Value = '4 players needed to start a game'

	repeat 
		wait(1)
	until #game.Players:GetPlayers() >= 1
	


	for i = defaultIntermission,1,-1 do
		game.Workspace.Statement.Value = tostring("Intermission: " ..i.. " seconds")
		wait(1)
	end

	randomMap = mapModule[math.random(1,#mapModule)]
	game.Workspace.Statement.Value = "The map we will be playing is " ..randomMap

	map = game.Lighting.Maps:FindFirstChild(randomMap)
	map.Parent = game.Workspace.MapWorkspace

	wait(3)
	game.Workspace.Statement.Value = "Teleporting Players..."
	wait(3)


	local totalSpawns = 0

	for i,v in pairs (map.Spawns:GetChildren()) do
		if v:IsA("Part") then
			totalSpawns = totalSpawns + 1
		end
	end

	local randomSpawn = 0

	for i,v in pairs (game.Players:GetChildren()) do
		if v:IsA("Player") then
			randomSpawn = math.random(1,totalSpawns)
			for i,v in pairs (game.Players:GetChildren()) do
				if v:IsA("Player") then
					randomSpawn = math.random(1,totalSpawns)
					game.Workspace[v.Name]:MoveTo(map.Spawns:GetChildren()[randomSpawn].Position + Vector3.new(0,5,0))
				end
			end
		end
	end

	for i = tonumber(map.Length.Value), 1, -1 do

		game.Workspace.Statement.Value = "Time Left: " ..i.. " seconds"
		wait(1)
	end

	game.Workspace.Statement.Value = "Map Over"
    winnering()
		
	wait(10)
		
	onClicked()
		
	for i,v in pairs(game.Players:GetPlayers()) do
			if v:IsA("Player") then

			game.Workspace[v.Name]:MoveTo(game.Workspace.SpawnLocation.Position + Vector3.new(0,5,0))
		end
	end
		task.wait(5)

		map.Parent = game.Lighting.Maps
		end
end

Great, add the code I sent to the very bottom of that script and try it out. I am only going off what @Forummer said earlier where I need to call the function when a player dies and leaves the game.

still same not working i have added your code at the very bottom of the script and separate

image

I didn’t really check the script, I just noticed that it’s the only place where the number of players is being checked.

Any idea what to do bro which helps me to complete it ??

Any one please help me i been searching help and this is soo hard to find help i been waiting for my approval to get access to create post and finally i got access now waiting for some one help please take a look