This while loop is supposed to do a time countdown in the game, and then perform an operation when time has run out (declare one team the winner of the game).
The while loop runs on the condition that as long as a value in the game is false, it will countdown. It works until the value in the game becomes true, and then if the value becomes false again it will not resume the countdown, much less do anything else. Output was of no use or help either.
Here’s the code, could someone help me out?
local length = 300
local notif = game.ReplicatedStorage:WaitForChild("SystemMessage")
local Taken = script:WaitForChild("taken")
---other variable declarations that aren't relevant to this section of code here
--[[
Functions such as onTouch(), Changed(), etc. here, before the while loop.
--]]
function RebalanceTeams()
---- balances teams
length = 300
end
while not Taken.Value do
if length > 0 then
length = length - 1
for i,v in ipairs(game.Players:GetChildren()) do
local seconds = length
local minutes = (length - length%60)/60
seconds = seconds - minutes*60
local gui = v.PlayerGui.main.Frame.time
gui.Text = minutes..":"..seconds
end
wait(1)
else
notif:FireAllClients("[SERVER]: The Police have successfully defended the package!",Color3.new(241,43,53))
notif:FireAllClients("[SERVER]: Game restarting...",Color3.new(241,43,53))
RebalanceTeams()
end
end
So that’s the condition for your while loop. If that ever becomes true then your loop will stop, permanently. You should start your loop via a function and run that function every time the condition is false again.
Alternatively you could do
while wait(1) do
if not Taken.Value then
if length > 0 then
length = length - 1
for i,v in ipairs(game.Players:GetChildren()) do
local seconds = length
local minutes = (length - length%60)/60
seconds = seconds - minutes*60
local gui = v.PlayerGui.main.Frame.time
gui.Text = minutes..":"..seconds
end
else
notif:FireAllClients("[SERVER]: The Police have successfully defended the package!",Color3.new(241,43,53))
notif:FireAllClients("[SERVER]: Game restarting...",Color3.new(241,43,53))
RebalanceTeams()
end
end
end
Oh, I didn’t know that while loops, when stopped, stopped permanently, for some reason I always thought they worked continuously. Oof.
Would this work instead?
while wait(1) do
if not Taken.Value then
if length > 0 then
length = length - 1
for i,v in ipairs(game.Players:GetChildren()) do
local seconds = length
local minutes = (length - length%60)/60
seconds = seconds - minutes*60
local gui = v.PlayerGui.main.Frame.time
gui.Text = minutes..":"..seconds
end
else
notif:FireAllClients("[SERVER]: The Police have successfully defended the package!",Color3.new(241,43,53))
notif:FireAllClients("[SERVER]: Game restarting...",Color3.new(241,43,53))
RebalanceTeams()
end
end
end