Work's perfectly fine the first time. In the 2nd time, it prints one of the 2 players is the only person surviving even though nobody died and ends the round.
function servercountdown()
print("Server count down called.")
-- Variables
values = game:GetService('ReplicatedStorage').Values
remote_events = game:GetService('ReplicatedStorage').RemoteEvents
Countdown = remote_events.Countdown
roundCountdown = remote_events.roundCountdown
remoteRoundEnded = remote_events.roundEnded
remoteRoundEndedPlr = remote_events.roundEndedPlr
removeShopGUI = remote_events.removeShopGUI
bringShopGUI = remote_events.bringShopGUI
module = require(script.module)
wait(1)
interval = 1
length = 30
minimum = 0
repeatdb = false
coindb = false
roundEnded = false
roundDuration = values:FindFirstChild('roundDuration')
roundMax = roundDuration.Value
mapFolder = game:GetService('ReplicatedStorage').Maps
-- Insert maps into table
maps = {}
for _,map in pairs(mapFolder:GetChildren()) do
table.insert(maps,1,map)
end
print(#maps)
-- Countdown script
-- Intermission, round picking & teleporting
wait(2)
roundEnded = false
if repeatdb == false then
repeatdb = true
for i = length,minimum,-1 do
local intermissionTime = game:GetService('ReplicatedStorage').Values:WaitForChild('IntermissionTime')
intermissionTime.Value = i
Countdown:FireAllClients(i)
wait(1)
if roundEnded == true then
break
end
end
print("Intermission over!")
wait(2)
local map = maps[math.random(1,#maps)]
map.Parent = workspace
local players = game.Players:GetPlayers()
module.TeleportAll(map.PrimaryPart)
removeShopGUI:FireAllClients()
for _,player in pairs(players) do
inRound = Instance.new("BoolValue",player)
inRound.Name = "InRound"
inRound.Value = true
end
-- Round management, round ending, awarding
for i = 120,0,-1 do
wait(1)
roundDuration.Value -= 1
roundCountdown:FireAllClients(i)
print(roundEnded)
local plrcount = 0
for _,player in pairs(game.Players:GetChildren()) do
if player:FindFirstChild('InRound') then
if player.InRound.Value == true then
plrcount += 1
print(plrcount)
if plrcount == 0 then
print("The player count is 0!")
end
end
end
end
local playertable = {}
for _,player in pairs(game.Players:GetChildren()) do
table.insert(playertable,1,player)
print(#playertable)
if plrcount == (#playertable-1) and coindb == false and roundEnded == false then
coindb = true
print("Round ended due to 1 player only named "..player.Name)
remoteRoundEndedPlr:FireAllClients(player)
player.leaderstats.Coins.Value += 10
game.ReplicatedStorage.Values.IntermissionTime.Value = 30
game.ReplicatedStorage.Values.roundDuration.Value = 120
workspace[player.Name].HumanoidRootPart.CFrame = workspace.SpawnLocation.CFrame
player.InRound:Destroy()
repeatdb = false
print("Round ended")
map.Parent = game.ReplicatedStorage.Maps
for _,gunplr in pairs(game.Players:GetChildren()) do
local lasergun = gunplr.Character:FindFirstChild("Laser Gun") or gunplr.Backpack:FindFirstChild("Laser Gun")
if not lasergun then
print("Laser gun not found, couldn't destroy.")
else
lasergun:Destroy()
end
end
roundEnded = true
coindb = false
repeatdb = false
playertable = {}
plrcount = 0
bringShopGUI:FireAllClients()
for _,player in pairs(game.Players:GetChildren()) do
if player.InRound then
player.InRound:Destroy()
end
end
break
end
end
for _,player in pairs(game.Players:GetChildren()) do
if player:WaitForChild("InRound") then
inRound = player.InRound
workspace[player.Name].Humanoid.Died:Connect(function()
inRound.Value = false
local lasergun = workspace:WaitForChild(player.Name):FindFirstChild("Laser Gun")
if lasergun then
workspace[player.Name].Humanoid:UnequipTools()
end
bringShopGUI:FireAllClients()
end)
end
end
if roundDuration.Value == 110 then
for _,player in pairs(game.Players:GetChildren()) do
if player:WaitForChild("InRound").Value == true then
local gunclone = game.ReplicatedStorage["Laser Gun"]:Clone()
gunclone.Parent = player.Backpack
end
end
end
if roundDuration.Value == 0 then
game.ReplicatedStorage.Values.IntermissionTime.Value = 30
game.ReplicatedStorage.Values.roundDuration.Value = 120
for _,player in pairs(players) do
if player.InRound then
player.InRound.Value = false
repeatdb = false
print("Round ended")
map.Parent = game.ReplicatedStorage.Maps
workspace[player.Name].HumanoidRootPart.CFrame = workspace.SpawnLocation.CFrame
roundEnded = true
for _,gunplr in pairs(game.Players:GetChildren()) do
local lasergun = gunplr.Character:FindFirstChild("Laser Gun") or gunplr.Backpack:FindFirstChild("Laser Gun")
if not lasergun then
print("Laser gun not found, couldn't destroy.")
else
lasergun:Destroy()
end
end
remoteRoundEnded:FireAllClients()
plrcount = 0
bringShopGUI:FireAllClients()
for _,player in pairs(game.Players:GetChildren()) do
if player.InRound then
player.InRound:Destroy()
end
end
break
end
end
end
end
end
end
servercountdown()
while wait() do
if roundEnded == true then
roundEnded = false
wait(2)
print("Restarting!")
servercountdown()
end
end
