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
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.
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.