Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there any way to make loops only last for a certain period of time?

Asked by 2 years ago
Edited 2 years ago

I'm trying to make this loop;

                repeat 
                    wait(.1)
                    print(check)
                    if check == 1 then
                        W = 0
                        break
                    end

                    until wait(W) or check == 1 or L == 1

(W, check, and L are all defined somewhere in my script beforehand, and W = 3 btw), and I was trying to make it repeat that code, UNTIL 3 seconds had passed, or check or L = 1. I then found out it repeats 1 time, then finishes after 3 seconds. So I then tried a while loop as while wait(W) do, thinking it would wait that time and run the code below until the time had passed, but it has the same issue as the repeat loop. Also the loop wont break if it only reads once. If anyone knows a way to have this loop last a period of time, comment or answer something if you wish. Thanks.

1 answer

Log in to vote
0
Answered by 2 years ago

Methods:

  1. Os time
  2. loop math

Os time:

local s = os.clock() -- os.time() works too, this is just a number thats always incrementing upwards.

repeat
-- code
until os.clock()-s > 1 -- one second has passed. I am basically repeating code until the current time is greater than the previous time

Loop math:

local t = 0
repeat wait(.1)
    t+=1 -- assuming this is ran 10 times, we wait .1 seconds before it adds again meaning when t is 10, 1 second will have passed.

    if t == 10 then
        break
    end
until

Note that there are more than 2 methods, I just listed popular ones with examples I made on the spot, pretty sure you could do polling or coroutines or some hacky workaround but these are nice ones to use

0
Yeah I was trying to do coroutines lol, this works, thanks. Stiles8353 35 — 2y
Ad

Answer this question