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...
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
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
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.
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!