#stopping a loop

1 messages · Page 1 of 1 (latest)

fervent orbit
#

whats the best way to instantly stop a loop that is currently in a timer? i currently used task.cancel and task.spawn but it looks so inefficient. any help will be appreciated thanks!

#

this is what i currently have

cosmic juniper
fervent orbit
cosmic juniper
#

is this client or server

fervent orbit
cosmic juniper
#

every 1 second at top of the script add a value

#

Do if statement and if its reaches 8 seconds then break

fervent orbit
cosmic juniper
#

Huh? That’s easier

fervent orbit
#

Also what if the script is (does something, waits 3 seconds, does another thing, wait 3)?

fervent orbit
#

Yes in a loop

cosmic juniper
#

task.wait(3) do something

fervent orbit
fervent orbit
cosmic juniper
#

if true then break end

#

Make a boolean

fervent orbit
cosmic juniper
fervent orbit
cosmic juniper
fervent orbit
cosmic juniper
#

how is there gonna be delay

fervent orbit
cosmic juniper
#

The if statement wont do nothing

#

until the condition is met

fervent orbit
#

I’m a little confused on what you’re suggesting, can you type a quick example?

cosmic juniper
#

Im on phone but sure

fervent orbit
#

Same ok

cosmic juniper
#

for count = 1,30 do
if timePassed == 8 then break end
Print(“code here”)
timepassed += 1
Task.wait(5)
end

#

@fervent orbit

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.

vast tuskBOT
#

studio** You are now Level 12! **studio

cosmic juniper
fervent orbit
fervent orbit
cosmic juniper
#

simple

fervent orbit
#

I’m not an idiot ik that

#

WHEN do I use the break in the loop

cosmic juniper
#

dude

#

what part do u not get about if statements

fervent orbit
#

omg

#

Js forget about it

#

I’ll ask someone else

cosmic juniper
#

how else u gonna check

#

ok

#

@chrome ether

chrome ether
chrome ether
chrome ether
fervent orbit
#

Is there a cleaner or more elegant solution or no?

chrome ether
#

no you either task.cancel/task.spawn pair or put a break condition in the loop

fervent orbit
#

So it wouldn’t work with waits right?

chrome ether
#

the wait would finish and then break

fervent orbit
#

As it would essentially add a cooldown before breaking

fervent orbit
chrome ether
#

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

fervent orbit
chrome ether
#

task.cancel closes any open thread

fervent orbit
chrome ether
#

no

fervent orbit
# chrome ether no

So task.cancel is better if I want it to stop the loop in its entirety right?

chrome ether
#

neither is strictly better

fervent orbit
#

I thought break would wait for the iteration to finish?

#

And I want to stop the entire iteration immediately

chrome ether
#

why not try some of these techniques out and see what happens instead of guessing

fervent orbit
burnt moss
fervent orbit
burnt moss
fervent orbit
#

It would only break after it reached that part of the iteration

cosmic juniper
#

yeah bro like everyone said use if statmenets

fervent orbit
cosmic juniper
fervent orbit
cosmic juniper
#

then make 2 loops

fervent orbit
#

With task.cancel