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

how do you break out of a while loop after a certain amount of time ?

Asked by 4 years ago

How do you break out of a while loop after a certain amount of time without making the loop run slower ?

2
use break to stop a loop Leamir 3138 — 4y
0
wth that wasnt my question Random_Haxer5 10 — 4y
2
no, use break DeceptiveCaster 3761 — 4y
0
sir that indeed was your question, if it wasnt, it was definitely worded poorly zadobyte 692 — 4y
View all comments (5 more)
0
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 10 — 4y
0
@Random_Haxer5 ... look at my answer below AlexTheCreator 461 — 4y
0
my r studio is down i cant test it Random_Haxer5 10 — 4y
0
You don't. zane21225 243 — 4y
0
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) cookie_more 30 — 4y

6 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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!

Ad
Log in to vote
0
Answered by 4 years ago

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
0
Also, I don't mean to copy ZethusImam. cookie_more 30 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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
Log in to vote
-1
Answered by
ACHRONlX 255 Moderation Voter
4 years ago

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

0
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) AlexTheCreator 461 — 4y
0
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** AlexTheCreator 461 — 4y
0
You have to put some kind of "wait()" instance in a loop, otherwise it'll crash your game. zane21225 243 — 4y
0
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... AlexTheCreator 461 — 4y
Log in to vote
-1
Answered by 4 years ago
Edited 4 years ago

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.

0
Also if you don't wanna use the math.abs you can do tick() - curt to get the positive value. greatneil80 2647 — 4y
Log in to vote
-1
Answered by
DesertusX 435 Moderation Voter
4 years ago
Edited 4 years ago

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!

Answer this question