How do you break out of a while loop after a certain amount of time without making the loop run slower ?
You could try this perhpas...
1 | i = tick() --start time |
2 | while tick() < i+ "DURATION IN SECONDS" do |
3 | wait() |
4 | --whatever |
5 |
6 | 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:
1 | 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]] -- |
2 | -- Put function here |
3 | end |
A simple but disadvantaged way you can do is this:
1 | local Time = 0 |
2 | while true do |
3 | wait( 0.1 ) --can be changed. |
4 | Time = Time + 1 --increases the time by 1. |
5 | if Time = = 15 then -- 15 * 0.1 = 1.5 seconds. Again, can be changed. |
6 | break --after 1.5 seconds it stops the while loop. |
7 | end |
8 | 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.
1 | 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. |
2 | print (i); |
3 | end |
Output: 1 2 3 4 5
I'll just point out the use of break with tick()
1 | local curt = tick() -- current tick, when game starts |
2 | while true do wait() |
3 | print ( "sup" ) |
4 | if math.abs(curt - tick()) > 5 then |
5 | break |
6 | end |
7 | 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.
I guess you could just do:
1 | for i = 10 , 0 , - 1 --Numeric for loop that counts down from 10. |
2 | wait() |
3 | end |
4 |
5 | while i = 0 do |
6 | wait() |
7 | print (i.. " seconds until loop breaks." ) |
8 | end |
Hope this helps!