How To Make It So The Last Player Alive Gets A Win?

Hey guys, I’m making a round based game and I am stuck. I am trying to make it so that the last player alive in the round gets rewarded with a win. How would I do that? Here is my code:

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

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

while true do
	
	--Intermission
	
	local Countdown = 30 -- intermission, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Intermission : '..Countdown
	until Countdown <= 0
	
	--Choose the map.
	
	Status.Value = 'Choosing Map...'
	
	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()


	wait(5) -- little pause, make this as long as you want
	
	ChosenMap.Parent = workspace
	Status.Value = 'Map chosen, teleporting players.'
	
	wait(2) -- little pause, make this as long as you want
	local Knife = ServerStorage.Knife
	--teleport the players
	
	local playersInRound = {} -- track alive players
	local connections = {} -- store connections to disconnect later
	for _, Player in pairs(Players:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			local RandomSpawn = Spawns[math.random(1, #Spawns)]
			Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
			local newKnife = Knife:Clone()
			newKnife.Parent = Player.Backpack
			
			table.insert(playersInRound, Player) -- add all players to table
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function() -- remove player from list on death
				table.remove(playersInRound, table.find(playersInRound, Player))
			end)
		end
	end
	
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	Countdown = 5 -- Starting Round In, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Starting Round in : '..Countdown
	until Countdown <= 0
	
	Countdown = 60 -- Game Time
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Ingame : '..Countdown
	until Countdown <= 0 or #playersInRound == 1
--Dax could you make a gui pop up here that makes the last player left, the winner, get 1 win added to win leaderstats and also have a gui pop up on everyones screens 
--that says the winner, and also awards the winner 100 coins
	
	--Kill the players
	for _, connection in pairs(connections) do -- disconnect connections to prevent memory leaks
		connection:Disconnect()
	end
	
	for _, Player in pairs(playersInRound)do
		Player:LoadCharacter()
	end
	
	ChosenMap:Destroy()
	
	Status.Value = 'Round Ended, waiting for new game.'
	
	wait(4) -- little pause, make this as long as you want.
	
end

This script is in startercharacterscripts and is a normal script. Thanks! Help is really appreciated.

1 Like

Everytime a player is removed, you can check with an if conditional,

if playersInRound[2] ~= nil then
      
else
    -- What to happen when last player is alive.
    print("The Winner Is: ".. playersInRound[1]) -- If you track the player instance instead of names use playersInRound[1].Name
end
2 Likes

In my code, where would I place that? @fastkingyaya And how would I add a leaderstat win to the player that was still alive?

			table.insert(playersInRound, Player) -- add all players to table
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
                if playersInRound[2] ~= nil then
                    -- Nothing
                else
                    -- What to happen when last player is alive
                    print("The Winner is: " ..playersInRound[1]) -- or playersInRound[1].Name
                    Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value + 1 -- I don't know if its called "Wins" but change it as you please
                end
			end)
2 Likes

Wait up, I’m a bit stuck in trying to insert that into my code. Could you maybe give me some directions @fastkingyaya?

I didn’t want to post the full think since it’s a bit messy but…

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

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

while true do
	
	--Intermission
	
	local Countdown = 30 -- intermission, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Intermission : '..Countdown
	until Countdown <= 0
	
	--Choose the map.
	
	Status.Value = 'Choosing Map...'
	
	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()


	wait(5) -- little pause, make this as long as you want
	
	ChosenMap.Parent = workspace
	Status.Value = 'Map chosen, teleporting players.'
	
	wait(2) -- little pause, make this as long as you want
	local Knife = ServerStorage.Knife
	--teleport the players
	
	local playersInRound = {} -- track alive players
	local connections = {} -- store connections to disconnect later
	for _, Player in pairs(Players:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			local RandomSpawn = Spawns[math.random(1, #Spawns)]
			Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
			local newKnife = Knife:Clone()
			newKnife.Parent = Player.Backpack

			table.insert(playersInRound, Player) -- add all players to table
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
                if playersInRound[2] ~= nil then
                    -- Nothing
                else
                    -- What to happen when last player is alive
                    print("The Winner is: " ..playersInRound[1]) -- or playersInRound[1].Name
                    Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value + 1 -- I don't know if its called "Wins" but change it as you please
                end
			end)
		end
	end
	
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	Countdown = 5 -- Starting Round In, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Starting Round in : '..Countdown
	until Countdown <= 0
	
	Countdown = 60 -- Game Time
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Ingame : '..Countdown
	until Countdown <= 0 or #playersInRound == 1
--Dax could you make a gui pop up here that makes the last player left, the winner, get 1 win added to win leaderstats and also have a gui pop up on everyones screens 
--that says the winner, and also awards the winner 100 coins
	
	--Kill the players
	for _, connection in pairs(connections) do -- disconnect connections to prevent memory leaks
		connection:Disconnect()
	end
	
	for _, Player in pairs(playersInRound)do
		Player:LoadCharacter()
	end
	
	ChosenMap:Destroy()
	
	Status.Value = 'Round Ended, waiting for new game.'
	
	wait(4) -- little pause, make this as long as you want.
	
end
3 Likes