Error when getting the round winner

I have a game and I need to get the round winner. Let’s say that all the players die at the same time. When that happens the server can’t get the winner and it error.

local _L = {}

local events = require(script.Parent.PlateEvents)

function _L.remove(player)
	if game.ReplicatedStorage.PlayersInRound:FindFirstChild(player.Name) then
		game.ReplicatedStorage.PlayersInRound:FindFirstChild(player.Name):Destroy()
		local tween = game:GetService("TweenService"):Create(workspace.RoundPlates:FindFirstChild(tostring(player.Name).."_plate"), TweenInfo.new(3), {Transparency = 1})
		tween:Play()
		player:WaitForChild("Status").Value = "Not Playing"
		wait(3)
		if workspace.RoundPlates:FindFirstChild(tostring(player.Name).."_plate") then
			workspace.RoundPlates:FindFirstChild(tostring(player.Name).."_plate"):Destroy()
		end
		
		game.ReplicatedStorage.Events.RoundText:FireClient(player)
	end
end

function _L.PlayerDied(player)
	if game.ReplicatedStorage.PlayersInRound:FindFirstChild(player.Name) then
		game.ReplicatedStorage.PlayersInRound:FindFirstChild(player.Name):Destroy()
		player:WaitForChild("Status").Value = "Not Playing"
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	if game.ReplicatedStorage.PlayersInRound:FindFirstChild(player.Name) then
		game.ReplicatedStorage.PlayersInRound:FindFirstChild(player.Name):Destroy()
	end
	
	if workspace.RoundPlates:FindFirstChild(tostring(player.Name).."_plate") then
		workspace.RoundPlates:FindFirstChild(tostring(player.Name).."_plate"):Destroy()
	end
end)

function _L.Intermission()
	repeat
		for i = 30, 0, -1 do
			game.ReplicatedStorage.Value.GameStatus.Value = "Intermission"
			game.ReplicatedStorage.Value.RoundTimer.Value = i
			wait(1)
		end
	until #game.Players:GetPlayers() >= 1
	
	game.ReplicatedStorage.Events.RoundText:FireAllClients()
	_L.Round()
end

function _L.Round()
	local dsb = game.ReplicatedStorage.Skyboxes.Forest:Clone()
	dsb.Name = "dsb"
	
	local rm = math.random(1, 2)

	if rm == 1 then
		game.ReplicatedStorage.Value.Map.Value = "Forest"
	elseif rm == 2 then
		game.ReplicatedStorage.Value.Map.Value = "Desert"
	end
	
	for i, player in pairs(game.Players:GetPlayers()) do
		local character = player.Character or player.CharacterAdded:Wait()
		local hrp = character:WaitForChild("HumanoidRootPart")
		
		-- Generate a random plate

		player:WaitForChild("Status").Value = "Playing"

		if game.ReplicatedStorage.Value.Map.Value == "Forest" then
			game.ReplicatedStorage.Skyboxes.Forest:Clone().Parent = game.Lighting
			
			local plates = workspace.Map.Forest.Plates:GetChildren()
			local random_plate = plates[math.random(1, #workspace.Map.Forest.Plates:GetChildren())]
			
			local plate_clone = game.ReplicatedStorage.Plates.ForestPlate:Clone()
			plate_clone.Parent = workspace.RoundPlates
			plate_clone.Name = tostring(player.Name).."_plate"
			plate_clone.BillboardGui.TextLabel.Text = tostring(player.Name).."'s plate"
			plate_clone.Position = random_plate.Position
			hrp.Position = Vector3.new(plate_clone.Position.X, plate_clone.Position.Y + 4, plate_clone.Position.Z)
			random_plate.Parent = game.ReplicatedStorage.RPlates
			local n = Instance.new("StringValue", game.ReplicatedStorage.PlayersInRound)
			n.Name = player.Name
			n.Value = player.Name
			
		elseif game.ReplicatedStorage.Value.Map.Value == "Desert" then
			game.ReplicatedStorage.Skyboxes.Desert:Clone().Parent = game.Lighting
			
			local plates = workspace.Map.Desert.Plates:GetChildren()
			local random_plate = plates[math.random(1, #workspace.Map.Desert.Plates:GetChildren())]
			
			local plate_clone = game.ReplicatedStorage.Plates.DesertPlate:Clone()
			plate_clone.Parent = workspace.RoundPlates
			plate_clone.Name = tostring(player.Name).."_plate"
			plate_clone.BillboardGui.TextLabel.Text = tostring(player.Name).."'s plate"
			plate_clone.Position = random_plate.Position
			hrp.Position = Vector3.new(plate_clone.Position.X, plate_clone.Position.Y + 4, plate_clone.Position.Z)
			random_plate.Parent = game.ReplicatedStorage.RPlates
			local n = Instance.new("StringValue", game.ReplicatedStorage.PlayersInRound)
			n.Name = player.Name
			n.Value = player.Name
		else
			print("ERR: Invalid map")
		end
	end
	
	repeat
		local randomNumber = math.random(1, 6)
		local randomPlate = workspace.RoundPlates:GetChildren()[math.random(1, #workspace.RoundPlates:GetChildren())]

		wait(5)
		if randomNumber == 1 then
			events.Transparency(randomPlate)
		elseif randomNumber == 2 then
			events.LowerPlate(randomPlate)
		elseif randomNumber == 3 then
			events.HigherPlate(randomPlate)
		elseif randomNumber == 4 then
			events.SpinningLava(randomPlate)
		elseif randomNumber == 5 then
			events.Jail(randomPlate)
		elseif randomNumber == 6 then
			events.TeleportTool()
		end
	until #game.ReplicatedStorage.PlayersInRound:GetChildren() == 2
	
	if not game.ReplicatedStorage.PlayersInRound:GetChildren()[1] then
		print("no winner")
		for i, player in pairs(game.Players:GetPlayers()) do
			local pgui = player:WaitForChild("PlayerGui")
			local gui = pgui:WaitForChild("RoundStatus")
			gui.Status.Text = "No winner"
		end
		
		wait(4)
		_L.Intermission()
	else
		local winner = game.ReplicatedStorage.PlayersInRound:GetChildren()[1]
		local fw = game.Players:FindFirstChild(winner.Name)

		fw.leaderstats.Wins.Value += 1
		fw.leaderstats.Coins.Value += math.random(10, 20)

		game.ReplicatedStorage.Events.EventText:FireAllClients("")

		game.ServerStorage.Events.Delete:Fire(dsb, winner)

		wait(4)
		_L.Intermission()
	end
end

return _L
1 Like

Hi David!

If what you said actually happens, you can try to determine the winner directly on the died function. It makes sense if two of the last players die, it removes the player value, and the server could have waited after and found no players remaining.

Also, it is possible that it could lead into an infinite yield due to the fact it can jump from three players to one before the loop checks.

until #game.ReplicatedStorage.PlayersInRound:GetChildren() == 2

Try replacing == with <= instead as a conditional statement. Also, If it’s a last person standing type of gamemode, the number should honestly be 1.

Let me know if this helped you or not and mark as solution if this is correct :smiley: