Just asking here because I have a while true do loop that just stops running right when the conditional statement within it is fulfilled (though it does not seem to run what is inside the conditional statement either), and I’m not sure what it could be getting stuck on. Any help would be greatly appreciated, and this is my code below:
while true do
wait()
print("test")
if game.Workspace.Values.Ongoing.Value == false then
for i, v in game.Workspace.Occupied:GetChildren() do
if v.Name ~= "PlaceHolder" then
v:Destroy()
end
end
for i = 15, 0, -1 do
wait(1)
print(i)
end
local AmountOfPlayers = 0
for i, v in game.Teams.Lobby:GetPlayers() do
AmountOfPlayers += 1
end
if AmountOfPlayers >= 2 then
for i, v in pairs(game:GetService("Players"):GetChildren()) do
local Plate = game.ReplicatedStorage.Plate:Clone()
local Occupied = true
local Found = false
while Occupied == true do
local column = math.random(0,8)
local row = math.random(0, 8)
for i, g in pairs(game.Workspace.Occupied:GetChildren()) do
if g.Name == column.."/"..row then
Found = true
break
else
Found = false
end
end
if Found == false then
Occupied = false
Plate.Position = Vector3.new((column*22)-88, 50, (row*22)-88)
Plate.Parent = game.Workspace.Box
Plate.Name = v.Character.Name
local NewFol = game.Workspace.Occupied.PlaceHolder:Clone()
NewFol.Name = column.."/"..row
NewFol.Parent = game.Workspace.Occupied
wait()
v.Character.HumanoidRootPart.CFrame = CFrame.new(Plate.Position)+Vector3.new(0,5,0)
v.Team = game.Teams.Playing
end
end
end
print("finished")
game.Workspace.Values.Ongoing.Value = true
game.Workspace.EventController.Start:Fire()
else
print("round cannot start not enough players")
end
end
end
Correct me if I’m wrong but does break not only terminate the current loop, which in this case would be the for loop? Which would not have any effect on the while loop as a whole. Also just to clarify, the first iteration of the loop runs flawlessly, It’s only after the conditional statement at the beginning is met for a second time that it breaks. For example, if I were to run it (providing there are enough players) , it would print test, test2, finished, and then the loop would print test until the conditional statement is met again, and then it would print nothing at all, which leads me to believe it is either stopping for some reason or getting stuck on something.
while task.wait() do
print('test')
if workspace.Values.Ongoing.Value == false then
for _, v in ipairs(workspace.Occupied:GetChildren()) do
if v.Name ~= "PlaceHolder" then
v:Destroy()
end
end
for i = 15, 0, -1 do
task.wait(1)
print(i)
end
if #game.Teams.Lobby:GetPlayers() >= 2 then
for _, v in ipairs(game:GetService('Players'):GetChildren()) do
local Plate = game.ReplicatedStorage.Plate:Clone()
local Occupied = true
local Found = false
repeat
local column = math.random(0,8)
local row = math.random(0, 8)
for _, g in ipairs(workspace.Occupied:GetChildren()) do
if g.Name == `{column}/{row}` then
Found = true
print('Still Occupied')
break
end
end
if Found == false then
Occupied = false
Plate.Position = Vector3.new((column*22)-88, 50, (row*22)-88)
Plate.Parent = workspace.Box
Plate.Name = v.Character.Name
local NewFol = workspace.Occupied.PlaceHolder:Clone()
NewFol.Name = `{column}/{row}`
NewFol.Parent = workspace.Occupied
v.Character.HumanoidRootPart.CFrame = CFrame.new(Plate.Position)+Vector3.new(0,5,0)
v.Team = game.Teams.Playing
end
until Occupied == false or task.wait() and false
end
print('finished')
workspace.Values.Ongoing.Value = true
workspace.EventController.Start:Fire()
else
print('round cannot start not enough players')
end
end
end
it prints test until the round ends and then stops printing anything at all, or more accurately it prints test, then finished, and then it repeats test until the round ends and stops.
Do simple prints before to see why if statements aren’t passing, for example:
while task.wait() do
-- print('test') -- not needed anymore, next line will print proving
-- that the while task.wait loop is repeating.
print("Ongoing.Value = ", workspace.Values.Ongoing.Value)
if workspace.Values.Ongoing.Value == false then
--code
Could you make record a video running the code, showing both the output and the explorer showing the Occupied Instance in workspace as well as whatever is inside?
I ended up just changing from a loop to a recursive function which I should have done in the first place. this message more or less made me realize how convoluted what I was trying to do was compared to the simplicity of the issue.