Hey guys, I just made a script where when everyone dies in a round, the round ends, but I realised that when one person was left alive, the round needs to end as well.
How could I make it so that when only one person is still alive in a round, the round ends and they are declared a winner, with a GUI coming up?
Also, how would I make it so that when they win, 1 win point is added to the wins leaderstats? Thanks!
Here’s my code:
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 = 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()
local RandomSpawn = Spawns[math.random(1, #Spawns)]
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
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 == 0
--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
Thanks! Help is really appreciated!