Hello! I am trying to make a script where you only have a certain amount of lives before you get teleported back to the lobby. What i’ve done so far:
-- variables
local rstorage = game:GetService("ReplicatedStorage")
local teams = game:GetService("Teams")
local sstorage = game:GetService("ServerStorage")
local statusChange = rstorage:WaitForChild("StatusChange")
local teamChoosing = rstorage:WaitForChild("TeamChoosing")
local teamTimeLeft = rstorage:WaitForChild("TeamTimeLeft")
local minPlayers = 2
local teamChoosingCooldown = 20
local gameLength = 180
local forcestart = false
local redTeam = teams.Red
local blueTeam = teams.Blue
local neutral = teams.Neutral
local function teamChoosingCountdown()
for i = teamChoosingCooldown, 0, -1 do
teamTimeLeft.Value = i
wait(1)
end
end
--listen for forcestart event for debugging
sstorage.ForceStart.Event:Connect(function()
forcestart = true
end)
-- Loop
function loop()
while wait(3) do
statusChange:FireAllClients({ title = 'GAME STATUS', body = 'Waiting for enough players' })
repeat wait() until #game.Players:GetPlayers() >= minPlayers or forcestart == true
forcestart = false
statusChange:FireAllClients({ title = 'INTERMISSION', body = 'Initializing game arena' })
wait(10)
statusChange:FireAllClients({ title = 'TEAM CHOOSING', body = "Please select the team that you'd like to join!" })
teamChoosing:FireAllClients()
teamChoosingCountdown()
local prevent1 = {}
local prevent2 = {}
for i, player: Player in pairs(game.Players:GetPlayers()) do
if player.Team == redTeam then
table.insert(prevent1, player)
elseif player.Team == blueTeam then
table.insert(prevent2, player)
end
end
if #prevent1 < 1 or #prevent2 < 1 then
statusChange:FireAllClients({ title = 'END OF GAME', body = 'Not enough players chose a team' })
for a, plr in pairs(prevent1) do
plr.Team = neutral
plr:LoadCharacter()
end
for a, plr in pairs(prevent2) do
plr.Team = neutral
end
loop()
return
end
wait(0.5)
local mapC = math.random(1, 2)
print(mapC)
if(mapC == 1) then
statusChange:FireAllClients({ title = 'NEXT MAP', body = 'MAP1' })
elseif(mapC == 2) then
statusChange:FireAllClients({ title = 'NEXT MAP', body = 'MAP2' })
end
wait(3)
statusChange:FireAllClients({ title = 'TELEPORTING', body = 'Teleporting players to the arena' })
for i, player: Player in pairs(game.Players:GetPlayers()) do
if player.Team == redTeam then
if mapC == 1 then
local redSpawns = workspace.Map.Spawns2.Red:GetChildren()
local selectedSpawn = redSpawns[math.random(1, #redSpawns)]
local clone = sstorage.StarterSword:Clone()
player:LoadCharacter()
task.wait(0.1)
clone.Parent = player.Backpack
player.Character.HumanoidRootPart.CFrame = selectedSpawn.CFrame
player.leaderstats['Games Played'].Value += 1
elseif mapC == 2 then
local redSpawns = workspace.Map.Spawns.Red:GetChildren()
local selectedSpawn = redSpawns[math.random(1, #redSpawns)]
local clone = sstorage.StarterSword:Clone()
player:LoadCharacter()
task.wait(0.1)
clone.Parent = player.Backpack
player.Character.HumanoidRootPart.CFrame = selectedSpawn.CFrame
player.leaderstats['Games Played'].Value += 1
end
elseif player.Team == blueTeam then
if mapC == 1 then
local blueSpawns = workspace.Map.Spawns2.Blue:GetChildren()
local selectedSpawn = blueSpawns[math.random(1, #blueSpawns)]
local clone = sstorage.StarterSword:Clone()
player:LoadCharacter()
task.wait(0.1)
clone.Parent = player.Backpack
player.Character.HumanoidRootPart.CFrame = selectedSpawn.CFrame
player.leaderstats['Games Played'].Value += 1
elseif mapC == 2 then
local blueSpawns = workspace.Map.Spawns.Blue:GetChildren()
local selectedSpawn = blueSpawns[math.random(1, #blueSpawns)]
local clone = sstorage.StarterSword:Clone()
player:LoadCharacter()
task.wait(0.1)
clone.Parent = player.Backpack
player.Character.HumanoidRootPart.CFrame = selectedSpawn.CFrame
player.leaderstats['Games Played'].Value += 1
end
end
end
wait(2)
for i = gameLength, 0, -1 do
local redPlrsRemaining = {}
local bluePlrsRemaining = {}
for x, player: Player in pairs(game.Players:GetPlayers()) do
if player.Team == redTeam and player.Character then
table.insert(redPlrsRemaining, player)
elseif player.Team == blueTeam and player.Character then
table.insert(bluePlrsRemaining, player)
end
end
local plrsRemaining = #redPlrsRemaining + #bluePlrsRemaining
statusChange:FireAllClients({ title = 'PLAYERS & TIME REMAINING', body = 'There are <b>'..plrsRemaining..'</b> players remaining and <b>'..i..'</b> seconds left' })
if #redPlrsRemaining == 0 and #bluePlrsRemaining == 0 then
statusChange:FireAllClients({ title = 'END OF GAME', body = 'Nobody won!' })
elseif #redPlrsRemaining == 0 and #bluePlrsRemaining ~= 0 then
-- Blue won
for _, plr in pairs(bluePlrsRemaining) do
plr.leaderstats.Wins.Value += 1
statusChange:FireAllClients({ title = 'END OF GAME', body = 'Blue team won!' })
plr.Team = neutral
plr:LoadCharacter()
plr.leaderstats.Lives.Value = 8
end
break;
elseif #bluePlrsRemaining == 0 and #redPlrsRemaining ~= 0 then
-- Red won
for _, plr in pairs(redPlrsRemaining) do
plr.leaderstats.Wins.Value += 1
statusChange:FireAllClients({ title = 'END OF GAME', body = 'Red team won!' })
plr.Team = neutral
plr:LoadCharacter()
plr.leaderstats.Lives.Value = 8
end
break
end
if plrsRemaining == 0 then
statusChange:FireAllClients({ title = 'END OF GAME', body = 'Nobody won!' })
break
elseif i == 0 then
statusChange:FireAllClients({ title = 'END OF GAME', body = 'Time is up!' })
break
end
wait(1)
end
wait(2)
for i, q in pairs(game.Players:GetPlayers()) do
if q:IsA('Player') then
q:LoadCharacter()
q.Team = neutral
print(q)
for i,v in pairs(q.Backpack:GetChildren()) do
if v:IsA('Tool') then
v:Destroy()
end
end
end
end
wait(2)
end
end
loop()
Thanks! <3