Round Win System not working

I’m having issues with the win system. Basically, the round system is working. If the round is over, the game checks how many players are left. If there is more than one player, the round is still going. But if there is only one player, it should say “Winner:” followed by the player’s name. And if there are no players left, it should say “No winners”. However, the main issue is that even when there is one player left, it still says “No winners” instead of displaying the player’s name. Can you please help me with this? Thank you.

Here’s the script:

local maps = game.ReplicatedStorage:WaitForChild("maps"):GetChildren()
local Status = game.ReplicatedStorage.Values:FindFirstChild("Status")
local spawnlobby = game.Workspace:FindFirstChild("SpawnLobbyPart")
local inround = game.ReplicatedStorage.Values:FindFirstChild("Inround")
--main game

local playersleft = {}

local intermission = 5
local ingameintermission  = 10

function maingame()
	-- Reset playersleft at the beginning of each round
	playersleft = {}

	for i = intermission, 0, -1 do
		inround.Value = false
		Status.Value = "Intermission: "..i
		task.wait(1)
	end
	if maps then
		local randomap = maps[math.random(1, #maps)]

		local clonedmap = randomap:Clone()
		clonedmap.Parent = game.Workspace

		Status.Value = "Chosen Map: "..clonedmap.Name

		wait(1)

		for i, v in pairs(game.Players:GetPlayers()) do
			if v.Character then
				v.Character.HumanoidRootPart.Position = clonedmap.PrimaryPart.Position
			end
		end
	end

	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			table.insert(playersleft, plr.Name)
			char.Humanoid.Died:Connect(function()
				table.remove(playersleft, table.find(playersleft, plr.Name))
			end)
		end)
	end)

	game.Players.PlayerRemoving:Connect(function(playeremoved)
		table.remove(playersleft, table.find(playersleft, playeremoved.Name))
	end)


	for i = ingameintermission, 0, -1 do
		inround.Value = true
		Status.Value = "InGame: "..i
		task.wait(1)
	end

	print(table.concat(playersleft, ";"))

	if  #playersleft == 0  then
		wait()
		Status.Value = "No Winners"

	elseif  #playersleft == 1 then
		wait()
		Status.Value = "Winners: Players: "..table.concat(playersleft, "Players: ")


	elseif  #playersleft > 1 then
		wait()
		Status.Value = "THE ROUND IS STILL GOING"
	end

end

-- Move playersleft outside of the maingame function
while wait() do
	wait(1)
	maingame()
end

It does look a bit messy, I would appreciate your feedback.

I am still pretty new to scripting so sorry for sounding uneducated, but can I ask why you got the playersleft table inside the function? Doesn’t that just reset it every second and that is why the game says “no winners?” And if you wanna reset the table, why not just use table.clear when necessary? It seems like a lot of unnecessary stuff inside one while loop. Oh and another thing, why the while wait() do wait(1) ? Why not just have one wait? btw I think task.wait() is better, don’t ask me why I’ve just been told.

local maps = game.ReplicatedStorage:WaitForChild("maps"):GetChildren()
local Status = game.ReplicatedStorage.Values:FindFirstChild("Status")
local spawnlobby = game.Workspace:FindFirstChild("SpawnLobbyPart")
local inround = game.ReplicatedStorage.Values:FindFirstChild("Inround")

local playersleft = {}

local intermission = 5
local ingameintermission  = 10

function maingame()
	-- Reset playersleft at the beginning of each round
	playersleft = {}

	for i = intermission, 0, -1 do
		inround.Value = false
		Status.Value = "Intermission: "..i
		task.wait(1)
	end
	if maps then
		local randomap = maps[math.random(1, #maps)]

		local clonedmap = randomap:Clone()
		clonedmap.Parent = game.Workspace

		Status.Value = "Chosen Map: "..clonedmap.Name

		wait(1)

		for i, v in pairs(game.Players:GetPlayers()) do
			if v.Character then
				v.Character.HumanoidRootPart.Position = clonedmap.PrimaryPart.Position
			end
		end
	end

	for _, player in pairs(game.Players:GetPlayers()) do
		spawn(function()
			table.insert(playersleft, player.Name)
			repeat task.wait() until game.Workspace:FindFirstChild(player.Name)
			local char = game.Workspace:FindFirstChild(player.Name)
			char.Humanoid.Died:Connect(function()
				table.remove(playersleft, table.find(playersleft, player.Name))
			end)
		end)
	end


	game.Players.PlayerRemoving:Connect(function(playeremoved)
		table.remove(playersleft, table.find(playersleft, playeremoved.Name))
	end)

	while true do
		for i = ingameintermission, 0, -1 do
			inround.Value = true
			Status.Value = "InGame: "..i
			task.wait(1)
		end

		print(table.concat(playersleft, ";"))

		if  #playersleft == 0  then
			wait()
			Status.Value = "No Winners"
			break
		elseif  #playersleft == 1 then
			wait()
			Status.Value = "Winners: Players: "..table.concat(playersleft, "Players: ")
			break
		elseif  #playersleft > 1 then
			wait()
			Status.Value = "THE ROUND IS STILL GOING"
		end
	end
end

-- Move playersleft outside of the maingame function
while wait() do
	wait(1)
	maingame()
end

Does this work?
Whethever it does or not, the problem is, that the maingame() function waits 5 seconds(intermission), before the game.Players.PlayerAdded function is connected to the signal. Means, the game doesnt notice if you join before or during the intermission.
If you use a for loop, the script loops through every player in the game and it doesnt matter when the player has joined. I also wrapped the main game inside a loop that only breaks if theres one or less players, so players that died dont get teleported into the main game again.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.