How can I end a round if there is 1 player left?

Hello, so I just finished my round system but I want the round to end if there is only 1 player left. How can I achieve this?
Here is my code:

while wait() do
	
	local players = {}
	
	status.Value = '4 players needed to start a game'
	
	repeat wait(.2) until game.Players.NumPlayers >= 4
	
	for i = time1, 0, -1 do
		status.Value = 'Starting in '..i..' seconds'
		wait(1)
	end
	
	startround()
	
	for i, v in pairs(game.Players:GetPlayers()) do
		table.insert(players, v)
	end
	
	
	for i = time2, 0, -1 do
		status.Value = i..' seconds left'
		wait(1)
	end
	
	endRound()
end

Thank you!

Do this:

if #players <= 1 then
    break
end
1 Like

Alternatively use while do loops properly.

while #players <= 1 do
     --// code
     wait()
end
1 Like

I actually did this:

while wait() do
	
	local players = {}
	
	status.Value = '4 players needed to start a game'
	
	repeat wait(.2) until game.Players.NumPlayers >= 2
	
	for i = time1, 0, -1 do
		status.Value = 'Starting in '..i..' seconds'
		wait(1)
	end
	
	startround()
	
	for i, v in pairs(game.Players:GetPlayers()) do
		table.insert(players, v)
	end
	
	
	for i = time2, 0, -1 do
		status.Value = i..' seconds left'
		for i, v in pairs(game.Players:GetPlayers()) do
			local character = v.Character
			character.Humanoid.Died:Connect(function()
				table.remove(players, v)
			end)
		end
		for i, v in pairs(players) do
			if #players >= 1 then
				endRound()
			end
		end
		wait(1)
	end
	
	endRound()
end

It’s working but i want to break this for loop:

	for i = time2, 0, -1 do
		status.Value = i..' seconds left'
		for i, v in pairs(game.Players:GetPlayers()) do
			local character = v.Character
			character.Humanoid.Died:Connect(function()
				table.remove(players, v)
			end)
		end
		for i, v in pairs(players) do
			if #players >= 1 then
				endRound()
			end
		end
		wait(1)
	end

Kind of like changing the index something like this:

i = 0,0,0

Is there a way to do that?

I dont really understand what you are trying to do.

1 Like

there is a break function maybe try that… Not really a function but just type in break

1 Like

Okay but how can I get the for loop, on a variable?

no…

for example this is your loop:

for i = 0,0,0 do

    --Code

    if #Players >= 1 then
        break
    end

end
1 Like

tell me if it worked… if it didn’t then show me what it says inside the output!

1 Like

I don’t really know how to add it to my script its soo confusing.

Ok where do you want to end the script? (end the for loop)

You seem to have defined a new table for players, which is 100% unnecessary!
```lua
game.Players:GetPlayers()

returns all the players edit misinterpretation of code, my bad

meaning you can do something like this

while wait() do
   status.value = '4 players needed to start a game'
   
   repeat 
      wait(1)
   until #game.Players:GetPlayers() >= 4
   
   for i = time1, 0, -1 do
		status.Value = 'Starting in '..i..' seconds'
		wait(1)
	end
	
	startround()
	
	for i, v in pairs(game.Players:GetPlayers()) do
		table.insert(players, v)
	end
	
	
	for i = time2, 0, -1 do
		status.Value = i..' seconds left'
		for i, v in pairs(game.Players:GetPlayers()) do
			local character = v.Character
			character.Humanoid.Died:Connect(function()
				table.remove(players, v)
			end)
		end
		
		if #players <= 1 then
			endRound()
            break
		end
		
		wait(1)
	end
   
   
end

As for ending, simply you can use

break

in order to stop a loop

1 Like

I tried printing #players and i noticed that its not removing players who died from the table is there a way to fix that?

Just do a playeradded event then a characteradded event then check if the humanoid dies by using Humanoid.Died and then remove the player from table

Edit: this is what I did in my round based game:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function()
			if table.find(PlayersInMatch, Player) then
				removePlayer(Player)
				print(Player.Name.. " has been removed")
			end
		end)
	end)
end)
1 Like

Is removePlayer(Player) a function?

No its a function that I made it just checks to see if the players is in the table and then removes them with:

table.remove()
1 Like

Then is there a function which cehcks the number of players? And checks if the number is 1.

1 Like

Well there is no function i think but you can just do:

if #players == 1 then
--code
end

I did this:

local module = require(script:WaitForChild('Functions'))

local status = game.ReplicatedStorage:WaitForChild('Status')

local players = {}

function removePlayer(p)
	for i, v in pairs(game.Players:GetPlayers()) do
		if v.Name == p.Name then
			table.remove(players, v)
		end
	end
end


game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function()
			if table.find(players, Player) then
				removePlayer(Player.Name)
				print(Player.Name.. " has been removed")
			end
		end)
	end)
end)

function startround()

	module.ChooseImposter()
	
	wait()
	
	module.TPplayers()
	
	for i, v in pairs(game.Players:GetPlayers()) do
		table.insert(players, v)
		print(v.Name..' Was added')
	end
end


function endRound(player)
	for i, v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild('ImposterValue') then
			v.ImposterValue:Destroy()
		end
	end
	module.TPplayersBack()
end

local time1 = 10

local time2 = 20

while wait() do
		
	status.Value = '4 players needed to start a game'
	
	repeat wait(.2) until game.Players.NumPlayers >= 1
	for i = time1, 0, -1 do
		status.Value = 'Starting in '..i..' seconds'
		wait(1)
	end
	
	startround()
	
	for i = time2, 0, -1 do
		status.Value = i..' seconds left'
		print(#players)
		if #players == 1 then
			break
		end
		wait(1)
	end
	
	endRound()
end

Its working with 1 player but when I test it with 2 players its not working.

You should call remove players here.

1 Like