#stopping a loop
1 messages · Page 1 of 1 (latest)
dude u can use break
Where?
is this client or server
This is an example script I made and I don’t see how it’s relevant, it’s a server script tho
every 1 second at top of the script add a value
Do if statement and if its reaches 8 seconds then break
Ik I could do that, the thing is I want it to be exact and that seems more complicated
Huh? That’s easier
Also what if the script is (does something, waits 3 seconds, does another thing, wait 3)?
for an for loop?
Yes in a loop
task.wait(3) do something
Lol what?
My question is how do I instantly stop a loop like this once a condition is met?
Yeah but there’s still a 6 second delay to stop the loop right?
dont over complicated it and do if statments to check if a condition is met and stop the loop
Where do I put the if statement?
inside the for loop
What if instead of 3 seconds it was 30 seconds… I don’t want any delay in stopping the loop
it doesnt make a difference
how is there gonna be delay
the if statement u suggested to stop the loop would still have to wait 60 seconds
I’m a little confused on what you’re suggesting, can you type a quick example?
Im on phone but sure
Same ok
for count = 1,30 do
if timePassed == 8 then break end
Print(“code here”)
timepassed += 1
Task.wait(5)
end
@fervent orbit
Bro u don’t uunderstand what im asking at all. The condition to stop the loop won’t be timer based, that was just an example. What I want is a way to instantly stop an ongoing for or while loop the second a condition is met.
** You are now Level 12! **
idk why you would post something irrelevant to your question but ok…
Like I said do an if statment inside the loop for whatever reason u want to check and thats it.
Like I said what if the loop has longer waits in them? The if statement would only be checked after the waits happen. I want the effect to happen immediately, not after it does a full loop
do an example of what u mean
How do I stop a loop like this immediately once a condition is met?
To stop a for loop you need to use break
simple
UWUDHWHIWIDBWHDUWUHWVDYSUA
I’m not an idiot ik that
WHEN do I use the break in the loop
when the if statement is true
dude
what part do u not get about if statements
why have i been summoned
task.cancel/task.spawn pair is not 'inefficient'
specially not for something like this, it's fine
Is there a cleaner or more elegant solution or no?
no you either task.cancel/task.spawn pair or put a break condition in the loop
Yeah the downside with the break condition is I usually have multiple waits
So it wouldn’t work with waits right?
the wait would finish and then break
As it would essentially add a cooldown before breaking
But it wouldn’t be instant?
it doesn't matter?
if the loop is going to immediately halt next time it resumes who cares?
if you have risk of overlapping timers then use the task.cancel/task.spawn pair
Does task.cancel stop a loop midway through or no?
task.cancel closes any open thread
So would it finish the iteration?
no
So task.cancel is better if I want it to stop the loop in its entirety right?
neither is strictly better
I thought break would wait for the iteration to finish?
And I want to stop the entire iteration immediately
I’m on my phone rn. My solution I showed worked but looked very unnesnesary for something quite simple. All I was wondering was if there was an easier solution but I guess not
how does s1 not know this
welp
There’s this nifty keyword called break
And where would u suggest to break the loop?
At a condition where you want to break the loop...
What if my loop has multiple waits?
It would only break after it reached that part of the iteration
yeah bro like everyone said use if statmenets
ts ain’t gonna work cuz I need it to be instant, im using task.cancel
break is instant sooo idk what u mean
it only checks the if once it reached that point in the iteration. If the iteration takes 1 minute the it’ll wait roughly 1 minute
then make 2 loops