I’m trying to make my rounds in my game loop, however cause it’s in a while true do the loop is the only part of the script active.
For example:
If a player leaves half way through the loop, the loop won’t stop. And the game will continue running even though there aren’t enough players.
I’m trying to make my rounds loop without sacrificing the rest of the script.
The script:
--[[vars]]--
local rep = game:GetService("ReplicatedStorage")
local guis = game:GetService("StarterGui")
local Players = game:GetService("Players")
local sss = game:GetService("ServerScriptService")
local timeVal = rep.Timer:WaitForChild("TimeVal")
local PlrAmountWarn = guis.PlayerWarn:WaitForChild("NotEnough")
local ShowNEP = rep.NEPEvents:WaitForChild("NotEnoughPlayers")
local RemoveNEP = rep.NEPEvents:WaitForChild("EnoughPlayers")
local SpinGame = rep.Games:WaitForChild("Spinner")
local SpleefGame = rep.Games:WaitForChild("Spleef")
local SpinTimeDefault = 120
local SpinTime = 120
local SpleefTimeDefault = 240
local SpleefTime = 240
--[[vars]]--
local function MapRemove()
if workspace:FindFirstChild("Spinner") then
workspace:FindFirstChild("Spinner"):Destroy()
else
print("Spinner not found.")
end
if workspace:FindFirstChild("Spleef") then
workspace:FindFirstChild("Spleef"):Destroy()
else
print("Spleef not found.")
end
end
--[[
FOR FUTURE USE.
CURRENTLY NOT NEEDED.
local function PlayTeam()
for i, v in pairs(Players:GetChildren()) do
if v:IsA("Player") then
v.Team = game.Teams:WaitForChild("Playing")
end
end
end
]]
local function TeamPlay()
for i, v in pairs(Players:GetPlayers()) do
--the if statement isn't needed, because we use :GetPlayers()
v.Team = game.Teams:WaitForChild("Playing")
v:LoadCharacter()
end
end
local function TeamLobby()
for i, v in pairs(Players:GetPlayers()) do
v.Team = game.Teams:WaitForChild('Lobby')
v:LoadCharacter()
end
end
local function Intermission()
timeVal.Value = "Intermission.. (15)"
task.wait(1)
timeVal.Value = "Intermission.. (14)"
task.wait(1)
timeVal.Value = "Intermission.. (13)"
task.wait(1)
timeVal.Value = "Intermission.. (12)"
task.wait(1)
timeVal.Value = "Intermission.. (11)"
task.wait(1)
timeVal.Value = "Intermission.. (10)"
task.wait(1)
timeVal.Value = "Intermission.. (9)"
task.wait(1)
timeVal.Value = "Intermission.. (8)"
task.wait(1)
timeVal.Value = "Intermission.. (7)"
task.wait(1)
timeVal.Value = "Intermission.. (6)"
task.wait(1)
timeVal.Value = "Intermission.. (5)"
task.wait(1)
timeVal.Value = "Intermission.. (4)"
task.wait(1)
timeVal.Value = "Intermission.. (3)"
task.wait(1)
timeVal.Value = "Intermission.. (2)"
task.wait(1)
timeVal.Value = "Intermission.. (1)"
task.wait(1)
timeVal.Value = "Intermission.. (0)"
task.wait(1)
end
timeVal.Value = " " -- Setting Timer to default 0
for i, v in pairs(Players:GetChildren()) do
if v:IsA("Player") then
v.Team = game.Teams:WaitForChild("Lobby")
end
end
Players.PlayerAdded:Connect(function(player, teamColor, character) -- Checking when a new player joins.
print(player.Name .. " joined the game!")
player.TeamColor = BrickColor.new("Mid gray")
local players = game:GetService("Players"):GetPlayers()
if #players >= 2 then
print("Enough players.")
RemoveNEP:FireAllClients() -- Removing NEP GUI
while true do -- the problem
Intermission()
timeVal.Value = "Loading..."
SpinGame:Clone().Parent = workspace
wait(SpinGame)
TeamPlay()
for i, v in pairs(game.Players:GetChildren()) do
local Character = v.Character or v.CharacterAdd:Wait(2)
local humanoid = Character:WaitForChild("Humanoid")
---Then just change there speed here like
humanoid.WalkSpeed = 0
humanoid.JumpPower = 80
end
task.wait(1)
timeVal.Value = "Starting... 3"
task.wait(1)
timeVal.Value = "Starting... 2"
task.wait(1)
timeVal.Value = "Starting... 1"
task.wait(1)
timeVal.Value = "Good luck!"
workspace:FindFirstChild("Spinner").Kill.Spinning.Disabled = false
task.wait(1)
local playersOnTeam = game:GetService('Teams').Playing
timeVal.Value = SpinTimeDefault
repeat
timeVal.Value = SpinTime
SpinTime = SpinTime - 1
task.wait(1)
until SpinTime == 0 or #playersOnTeam:GetPlayers() == 1 or #Players:GetPlayers() == 1
SpinTime = SpinTimeDefault
TeamLobby()
for i, v in pairs(game.Players:GetChildren()) do
local Character = v.Character or v.CharacterAdd:Wait(2)
local humanoid = Character:WaitForChild("Humanoid")
---Then just change there speed here like
humanoid.WalkSpeed = 16
humanoid.JumpPower = 50
end
-- Removing all maps
MapRemove()
Intermission()
timeVal.Value = "Loading..."
SpleefGame:Clone().Parent = workspace
wait(SpleefGame)
TeamPlay()
task.wait(1)
timeVal.Value = "Starting... 3"
task.wait(1)
timeVal.Value = "Starting... 2"
task.wait(1)
timeVal.Value = "Starting... 1"
task.wait(1)
timeVal.Value = "Good luck!"
workspace:FindFirstChild("Spleef").StartingPlat.CanCollide = false
task.wait(1)
timeVal.Value = SpleefTimeDefault
repeat
timeVal.Value = SpleefTime
SpleefTime = SpleefTime - 1
task.wait(1)
until SpleefTime == 0 or #playersOnTeam:GetPlayers() == 1 or #Players:GetPlayers() == 1
SpleefTime = SpleefTimeDefault
TeamLobby()
-- Removing all maps
MapRemove()
end
else -- mid
print("Not enough players.")
MapRemove()
ShowNEP:FireAllClients() -- Showing NEP GUI
timeVal.Value = ""
end
end)
Players.PlayerRemoving:Connect(function(player) -- Checking when a player leaves.
print(player.Name .. " left the game!")
local players = game:GetService("Players"):GetPlayers()
if #players == 1 then
print("Not enough players.")
ShowNEP:FireAllClients() -- Showing NEP GUI
timeVal.Value = ""
MapRemove()
end
end)
Please ignore my messy scripting.
Let me know if there’s a way to tell a script to go back up to a line. In this case, the script would go back to line 114. (if that makes sense)
Please, let me know if you have any questions! I will be more than happy to answer them.
Looking at the code, it doesn’t look like you have a check to see if a game is already running. If someone joins mid-game, then there would be 2 games running at the same time, which would cause problems.
You should move the while true do to after the Players.PlayerRemoving. Every game loop, check if there is enough players. If there isn’t, have something similar to repeat task.wait() until #players >= 2. Also, during the timer loop, have another check for the amount of players. If there isn’t, stop the timer.
If you want to figure out which players are in the game, and which are not, you can have a table that you can add to when a player joins the game during intermission/lobby, and remove from when a player leaves.
If you want to do multiple loops at the same time, you can use coroutines.
while true do
local players = game:GetService("Players"):GetPlayers()
if #players < 2 then
repeat task.wait() until #players >= 2
end
-- Game loop code can go here
end
Sorry for the poor formatting, I’m not exactly sure how to indent on the Developer Forum. Also, I didn’t notice you did actually have a check for if the player amount goes under 2, and that you had a table for the amount of players in-game. #playersOnTeam:GetPlayers() == 1 or #Players:GetPlayers() == 1
If your having a poor time understanding coroutines, you can also use spawn(function() to start new loops.
You should completely change the Players.PlayerAdded and Players.PlayerRemoving sections. The loop should start at the end of the script. The GUI code should be in the loop. Full code example:
--[[vars]]--
local rep = game:GetService("ReplicatedStorage")
local guis = game:GetService("StarterGui")
local Players = game:GetService("Players")
local sss = game:GetService("ServerScriptService")
local timeVal = rep.Timer:WaitForChild("TimeVal")
local PlrAmountWarn = guis.PlayerWarn:WaitForChild("NotEnough")
local ShowNEP = rep.NEPEvents:WaitForChild("NotEnoughPlayers")
local RemoveNEP = rep.NEPEvents:WaitForChild("EnoughPlayers")
local SpinGame = rep.Games:WaitForChild("Spinner")
local SpleefGame = rep.Games:WaitForChild("Spleef")
local SpinTimeDefault = 120
local SpinTime = 120
local SpleefTimeDefault = 240
local SpleefTime = 240
--[[vars]]--
local function MapRemove()
if workspace:FindFirstChild("Spinner") then
workspace:FindFirstChild("Spinner"):Destroy()
else
print("Spinner not found.")
end
if workspace:FindFirstChild("Spleef") then
workspace:FindFirstChild("Spleef"):Destroy()
else
print("Spleef not found.")
end
end
--[[
FOR FUTURE USE.
CURRENTLY NOT NEEDED.
local function PlayTeam()
for i, v in pairs(Players:GetChildren()) do
if v:IsA("Player") then
v.Team = game.Teams:WaitForChild("Playing")
end
end
end
]]
local function TeamPlay()
for i, v in pairs(Players:GetPlayers()) do
--the if statement isn't needed, because we use :GetPlayers()
v.Team = game.Teams:WaitForChild("Playing")
v:LoadCharacter()
end
end
local function TeamLobby()
for i, v in pairs(Players:GetPlayers()) do
v.Team = game.Teams:WaitForChild('Lobby')
v:LoadCharacter()
end
end
local function Intermission()
timeVal.Value = "Intermission.. (15)"
task.wait(1)
timeVal.Value = "Intermission.. (14)"
task.wait(1)
timeVal.Value = "Intermission.. (13)"
task.wait(1)
timeVal.Value = "Intermission.. (12)"
task.wait(1)
timeVal.Value = "Intermission.. (11)"
task.wait(1)
timeVal.Value = "Intermission.. (10)"
task.wait(1)
timeVal.Value = "Intermission.. (9)"
task.wait(1)
timeVal.Value = "Intermission.. (8)"
task.wait(1)
timeVal.Value = "Intermission.. (7)"
task.wait(1)
timeVal.Value = "Intermission.. (6)"
task.wait(1)
timeVal.Value = "Intermission.. (5)"
task.wait(1)
timeVal.Value = "Intermission.. (4)"
task.wait(1)
timeVal.Value = "Intermission.. (3)"
task.wait(1)
timeVal.Value = "Intermission.. (2)"
task.wait(1)
timeVal.Value = "Intermission.. (1)"
task.wait(1)
timeVal.Value = "Intermission.. (0)"
task.wait(1)
end
timeVal.Value = " " -- Setting Timer to default 0
for i, v in pairs(Players:GetChildren()) do
if v:IsA("Player") then
v.Team = game.Teams:WaitForChild("Lobby")
end
end
Players.PlayerAdded:Connect(function(player)
print(player.Name .. " joined the game!")
-- Assign the player to the Lobby team here
end)
Players.PlayerRemoving:Connect(function(player)
print(player.Name .. " left the game!")
-- Remove the player from the currently playing table here
end)
while true do
local players = game:GetService("Players"):GetPlayers()
if #players < 2 then
repeat task.wait() until #players >= 2
end
RemoveNEP:FireAllClients() -- Removing NEP GUI
-- Game loop code goes here
ShowNEP:FireAllClients() -- Showing NEP GUI
end
--[[vars]]--
local rep = game:GetService("ReplicatedStorage")
local guis = game:GetService("StarterGui")
local Players = game:GetService("Players")
local sss = game:GetService("ServerScriptService")
local timeVal = rep.Timer:WaitForChild("TimeVal")
local PlrAmountWarn = guis.PlayerWarn:WaitForChild("NotEnough")
local ShowNEP = rep.NEPEvents:WaitForChild("NotEnoughPlayers")
local RemoveNEP = rep.NEPEvents:WaitForChild("EnoughPlayers")
local SpinGame = rep.Games:WaitForChild("Spinner")
local SpleefGame = rep.Games:WaitForChild("Spleef")
local SpinTimeDefault = 120
local SpinTime = 120
local SpleefTimeDefault = 240
local SpleefTime = 240
--[[vars]]--
local function MapRemove()
if workspace:FindFirstChild("Spinner") then
workspace:FindFirstChild("Spinner"):Destroy()
else
print("Spinner not found.")
end
if workspace:FindFirstChild("Spleef") then
workspace:FindFirstChild("Spleef"):Destroy()
else
print("Spleef not found.")
end
end
--[[
FOR FUTURE USE.
CURRENTLY NOT NEEDED.
local function PlayTeam()
for i, v in pairs(Players:GetChildren()) do
if v:IsA("Player") then
v.Team = game.Teams:WaitForChild("Playing")
end
end
end
]]
local function TeamPlay()
for i, v in pairs(Players:GetPlayers()) do
--the if statement isn't needed, because we use :GetPlayers()
v.Team = game.Teams:WaitForChild("Playing")
v:LoadCharacter()
end
end
local function TeamLobby()
for i, v in pairs(Players:GetPlayers()) do
v.Team = game.Teams:WaitForChild('Lobby')
v:LoadCharacter()
end
end
local function Intermission()
timeVal.Value = "Intermission.. (15)"
task.wait(1)
timeVal.Value = "Intermission.. (14)"
task.wait(1)
timeVal.Value = "Intermission.. (13)"
task.wait(1)
timeVal.Value = "Intermission.. (12)"
task.wait(1)
timeVal.Value = "Intermission.. (11)"
task.wait(1)
timeVal.Value = "Intermission.. (10)"
task.wait(1)
timeVal.Value = "Intermission.. (9)"
task.wait(1)
timeVal.Value = "Intermission.. (8)"
task.wait(1)
timeVal.Value = "Intermission.. (7)"
task.wait(1)
timeVal.Value = "Intermission.. (6)"
task.wait(1)
timeVal.Value = "Intermission.. (5)"
task.wait(1)
timeVal.Value = "Intermission.. (4)"
task.wait(1)
timeVal.Value = "Intermission.. (3)"
task.wait(1)
timeVal.Value = "Intermission.. (2)"
task.wait(1)
timeVal.Value = "Intermission.. (1)"
task.wait(1)
timeVal.Value = "Intermission.. (0)"
task.wait(1)
end
timeVal.Value = " " -- Setting Timer to default 0
for i, v in pairs(Players:GetChildren()) do
if v:IsA("Player") then
v.Team = game.Teams:WaitForChild("Lobby")
end
end
Players.PlayerAdded:Connect(function(player)
print(player.Name .. " joined the game!")
player.Team = game.Teams:WaitForChild("Lobby")
end)
Players.PlayerRemoving:Connect(function(player)
print(player.Name .. " left the game!")
end)
while true do
local players = game:GetService("Players"):GetPlayers()
if #players >= 2 then
repeat task.wait() until #players >= 2
end
RemoveNEP:FireAllClients() -- Removing NEP GUI
Intermission()
timeVal.Value = "Loading..."
SpinGame:Clone().Parent = workspace
wait(SpinGame)
TeamPlay()
for i, v in pairs(game.Players:GetChildren()) do
local Character = v.Character or v.CharacterAdd:Wait(2)
local humanoid = Character:WaitForChild("Humanoid")
---Then just change there speed here like
humanoid.WalkSpeed = 0
humanoid.JumpPower = 80
end
task.wait(1)
timeVal.Value = "Starting... 3"
task.wait(1)
timeVal.Value = "Starting... 2"
task.wait(1)
timeVal.Value = "Starting... 1"
task.wait(1)
timeVal.Value = "Good luck!"
workspace:FindFirstChild("Spinner").Kill.Spinning.Disabled = false
task.wait(1)
local playersOnTeam = game:GetService('Teams').Playing
timeVal.Value = SpinTimeDefault
repeat
timeVal.Value = SpinTime
SpinTime = SpinTime - 1
task.wait(1)
until SpinTime == 0 or #playersOnTeam:GetPlayers() == 1 or #Players:GetPlayers() == 1
SpinTime = SpinTimeDefault
TeamLobby()
for i, v in pairs(game.Players:GetChildren()) do
local Character = v.Character or v.CharacterAdd:Wait(2)
local humanoid = Character:WaitForChild("Humanoid")
---Then just change there speed here like
humanoid.WalkSpeed = 16
humanoid.JumpPower = 50
end
-- Removing all maps
MapRemove()
Intermission()
timeVal.Value = "Loading..."
SpleefGame:Clone().Parent = workspace
wait(SpleefGame)
TeamPlay()
task.wait(1)
timeVal.Value = "Starting... 3"
task.wait(1)
timeVal.Value = "Starting... 2"
task.wait(1)
timeVal.Value = "Starting... 1"
task.wait(1)
timeVal.Value = "Good luck!"
workspace:FindFirstChild("Spleef").StartingPlat.CanCollide = false
task.wait(1)
timeVal.Value = SpleefTimeDefault
repeat
timeVal.Value = SpleefTime
SpleefTime = SpleefTime - 1
task.wait(1)
until SpleefTime == 0 or #playersOnTeam:GetPlayers() == 1 or #Players:GetPlayers() == 1
SpleefTime = SpleefTimeDefault
TeamLobby()
-- Removing all maps
MapRemove()
ShowNEP:FireAllClients() -- Showing NEP GUI
end
For the NEP GUI, you can have it appear at the start of the loop inside of the players check, and then remove it once enough players is reached. However, I do not know why the loop isn’t stopping, it should stop the timer and then wait for enough players at the start of the loop. What do you mean by “not stopping”? Are you talking about the timer or the entire loop?
--[[vars]]--
local rep = game:GetService("ReplicatedStorage")
local guis = game:GetService("StarterGui")
local Players = game:GetService("Players")
local sss = game:GetService("ServerScriptService")
local timeVal = rep.Timer:WaitForChild("TimeVal")
local PlrAmountWarn = guis.PlayerWarn:WaitForChild("NotEnough")
local ShowNEP = rep.NEPEvents:WaitForChild("NotEnoughPlayers")
local RemoveNEP = rep.NEPEvents:WaitForChild("EnoughPlayers")
local SpinGame = rep.Games:WaitForChild("Spinner")
local SpleefGame = rep.Games:WaitForChild("Spleef")
local SpinTimeDefault = 120
local SpinTime = 120
local SpleefTimeDefault = 240
local SpleefTime = 240
--[[vars]]--
local function MapRemove()
if workspace:FindFirstChild("Spinner") then
workspace:FindFirstChild("Spinner"):Destroy()
else
print("Spinner not found.")
end
if workspace:FindFirstChild("Spleef") then
workspace:FindFirstChild("Spleef"):Destroy()
else
print("Spleef not found.")
end
end
--[[
FOR FUTURE USE.
CURRENTLY NOT NEEDED.
local function PlayTeam()
for i, v in pairs(Players:GetChildren()) do
if v:IsA("Player") then
v.Team = game.Teams:WaitForChild("Playing")
end
end
end
]]
local function TeamPlay()
for i, v in pairs(Players:GetPlayers()) do
--the if statement isn't needed, because we use :GetPlayers()
v.Team = game.Teams:WaitForChild("Playing")
v:LoadCharacter()
end
end
local function TeamLobby()
for i, v in pairs(Players:GetPlayers()) do
v.Team = game.Teams:WaitForChild('Lobby')
v:LoadCharacter()
end
end
local function Intermission()
timeVal.Value = "Intermission.. (15)"
task.wait(1)
timeVal.Value = "Intermission.. (14)"
task.wait(1)
timeVal.Value = "Intermission.. (13)"
task.wait(1)
timeVal.Value = "Intermission.. (12)"
task.wait(1)
timeVal.Value = "Intermission.. (11)"
task.wait(1)
timeVal.Value = "Intermission.. (10)"
task.wait(1)
timeVal.Value = "Intermission.. (9)"
task.wait(1)
timeVal.Value = "Intermission.. (8)"
task.wait(1)
timeVal.Value = "Intermission.. (7)"
task.wait(1)
timeVal.Value = "Intermission.. (6)"
task.wait(1)
timeVal.Value = "Intermission.. (5)"
task.wait(1)
timeVal.Value = "Intermission.. (4)"
task.wait(1)
timeVal.Value = "Intermission.. (3)"
task.wait(1)
timeVal.Value = "Intermission.. (2)"
task.wait(1)
timeVal.Value = "Intermission.. (1)"
task.wait(1)
timeVal.Value = "Intermission.. (0)"
task.wait(1)
end
timeVal.Value = " " -- Setting Timer to default 0
for i, v in pairs(Players:GetChildren()) do
if v:IsA("Player") then
v.Team = game.Teams:WaitForChild("Lobby")
end
end
Players.PlayerAdded:Connect(function(player)
print(player.Name .. " joined the game!")
player.Team = game.Teams:WaitForChild("Lobby")
end)
Players.PlayerRemoving:Connect(function(player)
print(player.Name .. " left the game!")
end)
while true do
local players = game:GetService("Players"):GetPlayers()
if #players >= 2 then
repeat task.wait() until #players >= 2
end
RemoveNEP:FireAllClients() -- Removing NEP GUI
Intermission()
timeVal.Value = "Loading..."
SpinGame:Clone().Parent = workspace
wait(SpinGame)
TeamPlay()
for i, v in pairs(game.Players:GetChildren()) do
local Character = v.Character or v.CharacterAdd:Wait(2)
local humanoid = Character:WaitForChild("Humanoid")
---Then just change there speed here like
humanoid.WalkSpeed = 0
humanoid.JumpPower = 80
end
task.wait(1)
timeVal.Value = "Starting... 3"
task.wait(1)
timeVal.Value = "Starting... 2"
task.wait(1)
timeVal.Value = "Starting... 1"
task.wait(1)
timeVal.Value = "Good luck!"
workspace:FindFirstChild("Spinner").Kill.Spinning.Disabled = false
task.wait(1)
local playersOnTeam = game:GetService('Teams').Playing
timeVal.Value = SpinTimeDefault
repeat
timeVal.Value = SpinTime
SpinTime = SpinTime - 1
task.wait(1)
until SpinTime == 0 or #playersOnTeam:GetPlayers() == 1 or #Players:GetPlayers() == 1
SpinTime = SpinTimeDefault
TeamLobby()
for i, v in pairs(game.Players:GetChildren()) do
local Character = v.Character or v.CharacterAdd:Wait(2)
local humanoid = Character:WaitForChild("Humanoid")
---Then just change there speed here like
humanoid.WalkSpeed = 16
humanoid.JumpPower = 50
end
-- Removing all maps
MapRemove()
Intermission()
timeVal.Value = "Loading..."
SpleefGame:Clone().Parent = workspace
wait(SpleefGame)
TeamPlay()
task.wait(1)
timeVal.Value = "Starting... 3"
task.wait(1)
timeVal.Value = "Starting... 2"
task.wait(1)
timeVal.Value = "Starting... 1"
task.wait(1)
timeVal.Value = "Good luck!"
workspace:FindFirstChild("Spleef").StartingPlat.CanCollide = false
task.wait(1)
timeVal.Value = SpleefTimeDefault
repeat
timeVal.Value = SpleefTime
SpleefTime = SpleefTime - 1
task.wait(1)
until SpleefTime == 0 or #playersOnTeam:GetPlayers() == 1 or #Players:GetPlayers() == 1
SpleefTime = SpleefTimeDefault
TeamLobby()
-- Removing all maps
MapRemove()
ShowNEP:FireAllClients() -- Showing NEP GUI
end
It’s only checking how many players are in the game at the start of the loop.
This error is happening because you’re defining the players variable here as the amount of players. @LoveingLiamGuy’s code was a bit wrong. It should be:
local players = game:GetService("Players")
if #players:GetPlayers() <= 2 then
-- not enough to start
repeat task.wait() until #players:GetPlayers() > 2
end