How Do I Break a Loop With Long Wait()'s

I have a while true do loop in my script and it has a wait(15) in it which is quite long, so if it’s started doing the loop when doneOrLate is equal to true, it will wait until the wait(15) is over before breaking the loop. Here’s the part of my script:

while true do
	-- I have code here but no wait()'s
	wait(15)
	if doneOrLate == true then
		break
	end
end
1 Like

I don’t understand what’s the point of it and I don’t understand what you’ve said. Sorry.

1 Like

Simply thread it

while true do
coroutine.resume(coroutine.create(function()
	wait(15)
	if doneOrLate == true then
		break
	end
end))
end
1 Like

I want it to check if doneOrLate is equal to true more often.

1 Like

That wouldn’t work, break can be, at most, one indent out, that’s two indents out.

1 Like
while true do
	-- I have code here but no wait()'s
	wait(15)
 doneOrLate:GetPropertyChangedSignal("Value"):Connect(function()
		break
end
	end
end

Maybe this will work?

1 Like

Also consider changing doneOrLate a bool value inside the script?

1 Like

doneOrLate is a variable inside the script, and also, once again;

1 Like

Insert a bool value inside the script.

1 Like

How do I insert a bool value inside the script?

1 Like

Do you mean parenting it under the script?

1 Like

STEP 1
image
You see that plus sign? Click on it.
STEP 2
image
Type in bool value.
STEP 3
image
Click bool value and your done creating it.
STEP 4
Last and final step
image
Just rename it.

It’s still indented though, it won’t work.

I think this should work?

local seconds = 15
local howMuchWait = 0.1 -- every 100 milliseconds
while true do
   for i=1, seconds / howMuchWait  do
      --this will break for loop if doneOrLate Variable true
      if doneOrLate == true then
         break
      end
      wait(howMuchWait)
   end
   --this will break while loop
   if doneOrLate == true then
      break
   end
end

Thought its not perfect since it will still wait some time before breaking loop. Minimum time you can set at howMuchWait variable is ~0.03 since its minimum time wait() can wait.

Where would I put my code?:arrow_down:

Oh, just put it on top.

Like you did before:

local seconds = 15
local howMuchWait = 0.1 -- every 100 milliseconds
while true do
   --put your code here

   for i=1, seconds / howMuchWait  do
      --this will break for loop if doneOrLate Variable true
      if doneOrLate == true then
         break
      end
      wait(howMuchWait)
   end
   --this will break while loop
   if doneOrLate == true then
      break
   end
end
7 Likes