Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

1 comment

0

Yeah I was trying to do coroutines lol, this works, thanks.

This discussion is preserved as it was. New answers, comments, and votes are closed.