End round when there is 1 player remaining

Hello!
I’m trying to make a round system, and it’s working rn but I want to add so that the round ends if there is 1 player remaining.

I’ve seen similar posts, but none of them worked as they were very old.

How could I do this?

local itemName = "Weapon"
local maps = game.ReplicatedStorage.Maps:GetChildren()
local status = game.ReplicatedStorage.Status
local gun = game.ReplicatedStorage.Weapon

while true do
task.wait(3)
	for i = 25, 0, -1 do
		status.Value = "Round starting in " ..i.. " seconds."
		task.wait(1)
	end

	local mapClone = maps[math.random(1, #maps)]:Clone()
	mapClone.Parent = game.Workspace
	local spawnPoints = mapClone.TpPoints:GetChildren()
	status.Value = "Mode: "..mapClone.Name
	local arenaPoints = game.Workspace.Arena.TpPoints:GetChildren()
	task.wait(3)
	
	for i, player in pairs(game.Players:GetPlayers()) do
			if player.Character then
			player.weaponStats.InRound.Value = true
			player.PlayerGui.Stats.Enabled = true
			local chosenTp
			repeat
				chosenTp = spawnPoints[math.random(1, #spawnPoints)]
			until chosenTp.Name ~= "Taken"
			player.Character.HumanoidRootPart.CFrame = chosenTp.CFrame
			chosenTp.Name = "Taken"
		end
	end
	

	
	for i = 120, 0, -1 do
		status.Value = "Loot up! "..i.." seconds remaining."
		task.wait(1)
	end
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player.Character and player.weaponStats.InRound.Value == true then
			local clone = gun:Clone()
			clone.Configurations.MinDamage.Value = player.weaponStats.Damage.Value
			clone.Configurations.MaxDamage.Value = player.weaponStats.Damage.Value
			clone.Parent = player.Backpack
			game.ReplicatedStorage.Remaining.Value = i
			local choosenTp
			repeat
				choosenTp = arenaPoints[math.random(1, #arenaPoints)]
			until choosenTp.Name ~= "Taken"
			player.Character.HumanoidRootPart.CFrame = choosenTp.CFrame
			choosenTp.Name = "Taken"
		end
	end
	
	
	for i = 180, 0, -1 do
		status.Value = "Fight! "..i.. " seconds remaining."
		task.wait(1)
	end
	

	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player.Character then
			player.Backpack:ClearAllChildren()
			if player.Character and player.Character:FindFirstChild(itemName) and player.Character[itemName]:IsA("Tool") then
				player.Character[itemName]:Destroy()
			end
				player.Character.HumanoidRootPart.CFrame = game.Workspace.Lobby.TpPoint.CFrame
			player.weaponStats.InRound.Value = false
				player.PlayerGui.Stats.Enabled = false
				player.weaponStats.Damage.Value = 20
			player.weaponStats.Health.Value = 100
			
		end
	end
	
	
	
	status.Value = "Round over."
	for i, v in pairs(spawnPoints) do
		v.Name = "TpPoint"
	end

	mapClone:Destroy()
	end

I actually had this issue last year and I was able to find a solution by myself. If you wanted to end the round when there is 1 player remaining you could do something similar to this

local team = game:GetService("Teams").Playing --Not sure if you have teams in this game but im sure you can configure this variable to suit your code
local teamCount = #team:GetPlayers() -- A hashtag (#) in Lua returns how many children or in this case players are associated with the object.

if teamCount <= 1 then
   roundEnd = true --Not sure how your code works and how you end the round but this is the overview of how to do it.
end

I hope this makes sense :smile:

2 Likes

Thank you so much! Works perfectly :slight_smile:

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