How do you break out of a while loop after a certain amount of time without making the loop run slower ?
how do you break out of a while loop after a certain amount of time ?
6 answers
You could try this perhpas...
i = tick() --start time
while tick() < i+"DURATION IN SECONDS" do
wait()
--whatever
end
Tick() is the number of seconds passed since 01/01/1970 so by checking the current tick compared to the start time (i) + the duration you want it should create a timer without using wait(1) i = i+1 etc.
Not sure if this is the best way but I think it'll work!
So, you need to make a function that creates the tween, and plays it. Then, put the function in a script like this:
for I = 1, 5, 1 do --[[ Note: The "5" is the amount of times you want it to loop. I just put it there as an example]]--
-- Put function here
end
1 comment
Also, I don't mean to copy ZethusImam.
A simple but disadvantaged way you can do is this:
local Time = 0
while true do
wait(0.1) --can be changed.
Time = Time + 1 --increases the time by 1.
if Time == 15 then -- 15 * 0.1 = 1.5 seconds. Again, can be changed.
break --after 1.5 seconds it stops the while loop.
end
end
I don't know what you're trying to do, but I'd recommend using a numeric for loop for this, it'll automatically stop when the given amount is reached.
for i = 1, 5, 1 do -- i is defined as the current number we are on, so it will chan each time, the first 1 is the start, the 5 is how many times we want it to do the loop and the other 1 is how many steps it takes.
print(i);
end
Output: 1 2 3 4 5
4 comments
I think this is part of the issue though, this loop does not expire by time it expires by how fast the loop can repeat itself. Sure you could put wait(1) in there to make sure each loop takes at least one second but that will slow down the process (which he is trying to avoid)
the loop does not expire by a set time it expires by the amount of time it takes for the loop to repeat itself "i" times
You have to put some kind of "wait()" instance in a loop, otherwise it'll crash your game.
ah yeah true, my method above has been fixed now. Again though, the method I've suggested relies on time passing rather than reaching the end of the loop...
I'll just point out the use of break with tick()
local curt = tick() -- current tick, when game starts
while true do wait()
print("sup")
if math.abs(curt - tick()) > 5 then
break
end
end
basically an original tick is made, a loop is formed, it prints sup a ton of times, if the last tick minus the current tick is greater than 5 seconds then break.
1 comment
Also if you don't wanna use the math.abs you can do tick() - curt to get the positive value.
I guess you could just do:
for i = 10, 0, -1 --Numeric for loop that counts down from 10.
wait()
end
while i = 0 do
wait()
print(i.." seconds until loop breaks.")
end
Hope this helps!
This discussion is preserved as it was. New answers, comments, and votes are closed.

9 comments
use break to stop a loop
wth that wasnt my question
no, use break
sir that indeed was your question, if it wasnt, it was definitely worded poorly
my question is how do you break out of a while loop after a certain amount of time without making the loop run slower ?
@Random_Haxer5 ... look at my answer below
my r studio is down i cant test it
You don't.
Use a for loop. Like this: for I = 1, 30, 1 do (that is the loop. The middle number is how many time it loops. Below the line of code I showed you, and "end" will appear, and put the code inside. If you don't understand, just look up a video on what a for loop is)