Ending a round once 1 player is left

Hello! I’m working on a game and I want to end a round once 1 player is left. I’ve got the script but have no idea how to do it. Help is needed! Thanks in advance. Code: (from a youtube video)

ServerStorage = game:GetService("ServerStorage")
ReplicatedStorage = game:GetService("ReplicatedStorage")
Players = game:GetService("Players")

Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
Status = ReplicatedStorage:WaitForChild('Status')

while true do

	--Intermission

	local Countdown = 300 

	repeat wait(1)
		Countdown = Countdown - 1

		Status.Value = 'Intermission : '..Countdown
		print(Status.Value)
	until Countdown <= 0

	--Choose the map.

	Status.Value = 'Choosing Map...'

	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()
	local RandomSpawn = Spawns[math.random(1, #Spawns)]

	wait(5) 

	ChosenMap.Parent = workspace
	Status.Value = 'Map chosen, get ready to fire your brick guns!'
	print(Status.Value)

	wait(2) 

	--teleport the players

	for _, Player in pairs(Players:GetChildren())do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
		end
	end

	Countdown = 5 -- Starting Round In, make this as long as you want

	repeat wait(1)
		Countdown = Countdown - 1

		Status.Value = 'The game will start in : '..Countdown
	until Countdown <= 0

	Countdown = 500 

	repeat wait(1)
		Countdown = Countdown - 1

		Status.Value = 'Time left : '..Countdown
		print(Status.Value)
	until Countdown <= 0

	--Kill the players

	for _, Player in pairs(Players:GetChildren())do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.Humanoid:TakeDamage(2000)
		end
	end

	ChosenMap:Destroy()

	Status.Value = 'Round Ended, waiting for new game.'
	print(Status.Value)

	wait(4) 

end

1 Like

Try adding all the characters to a table and then check if the table has only 1 person.

Assign all those who are playing into a Team, when they die you remove them of the Team.

In your Time Left loop you can check if there’s only 1 player in the ‘Playing’ team, you can do that with Team:GetPlayers()

Hello, make a list of players that are in-game first.

list_of_players = {}

Then edit your teleport system to make it adding players in list like a:

table.insert(list_of_players,player_that_is_being_teleported.Name)

After all players are in game and counted - intiate event. Event for every player - if player would die - delete them from list of players and check if there is anybody left. Once 1 person left- do something with winner.

for i = 1,#list_of_players do
    local player = game.Players:FindFirstChild(list_of_players[i])
    player.CharacterAdded:Connect(function() --Event fires when player respawns
        table.remove(list_of_players,table.find(list_of_players,player.Name)) --Finds player in table and deletes him
        if #list_of_players < 2 then
            local winner = game.Players:FindFirstChild(#list_of_players[1])
            -- Do something with winner
        end
    end)

Also, add section of code that would delete ppl leaving game. That’s it. Ofc don’t forget to disconnect events (if you know how to do that) to avoid multiple events on one player and don’t forget to nul the table.

2 Likes

Idk if I’m thinking of the correctly or not, but you could make a region3 that covers the area in which the round is taking place and check if there is only one humanoid left in the region? I’m not sure how to set that up, so just an idea for now.

1 Like

You could tag everyone with Collection Service then check how many people have the tag and if it is <= 1 then end the round

2 Likes

When I do that and set the remaining person as a variable, I cant change anything about it, I cant access leaderstats, etc.