Winners system bugged

Please help. its not calling function when all players are dead it must show there’s is no winner but its not working kindly help please


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

-------------------

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 == 0 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 >= 2 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
For Dead players i have mention this too

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)

Maybe store all the players that are alive beforehand in a table first first instead of continuously trying to get all the alive players after a player died. And when a player dies, remove the player from the table.

I also see that there is Humanoid.Died in winnering() but the winnering() is already called from Humanoid.Died so I would say remove the died player in the alive list in the Died event itself in the CharacterAdded portion.

So something like

local playersInRound = {}
for index, player in pairs(game.Players:GetChildren()) do
	-- if player is playing right now or is alive, do table.insert(playersInRound, player)
end
----------------------------------------------------------
function winnering()
	if #playersInRound == 0 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 >= 2 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
end
-----------------------------------------------------------
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local h = char:WaitForChild("Humanoid")
		h.Died:Connect(function()
			table.remove(player,playersInRound)
			winnering()
		end)

	end)
end)
game.Players.PlayerRemoving:Connect(function(player)
	table.remove(player,playersInRound)
	winnering()
end)
1 Like

@Psvita65594 brother i’ve tested your provided codes its keep showing There’s is no winner but still there’s is 1 player left it must call him winner

1 Like

I prefer counting the alive players beforehand.

---|Specifically this|-----------------
local playersInRound = {}
for index, player in pairs(game.Players:GetChildren()) do
	if player --[[is alive]] then 
		table.insert(playersInRound, player)
	end
end
-----------------------------------------
function winnering()
	if #playersInRound == 0 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 >= 2 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
end
-----------------------------------------------------------
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local h = char:WaitForChild("Humanoid")
		h.Died:Connect(function()
			table.remove(player,playersInRound)
			winnering()
		end)

	end)
end)
game.Players.PlayerRemoving:Connect(function(player)
	table.remove(player,playersInRound)
	winner


But if you want to count the alive players in the winnering() function, then I would declare a table of the non-alive players first. Further, in the winnering() function, when the player dies, the player is stored in the non-alive players table. After that compare the alive players table and non-alive players table and check to see if a specific number of players are not in the non-alive players table and if there is, then those specifc players wins and then award them.