Is there a way you can stop a repeat until loop with multiple methods

Hello developers I am trying to stop me repeat loop when a value hits zero or and even is fired how do I achieve this

2 Likes

do you mean like using until to stop the loop or can you please explain it more

2 Likes
repeat
-- code
until value.Value == 0

Put this in a suitable place, it will only run once the script reaches the repeat line. You can make a function to trigger the repeat however.

2 Likes

Something like this?

local stopped = false
local var = 100

repeat
  var = var - 1
  wait(1)
until var <= 0 or stopped

event:Connect(function() stopped = true end)
2 Likes

Let Me try that (extra characters because of devforums

I don’t think or works with repeat until

1 Like

It does. Just like an if statement.

1 Like

then would this work?
image

1 Like

image
image
For some reason its not stopping

That is because in the “if” statement after the repeat loop it is not checking the value of the time it is checking the instance.

1 Like

It was interfering with another function

I would recommend using a while loop over a repeat loop as they are much easier to read and provide the same functionality. By connecting your event before the loop is started, you can toggle a boolean that will break the loop in the event callback.

local Looping = true
Event:Once(function() -- :Once() will automatically disconnect the event after it has fired, similar to calling `:Disconnect()` (however this method is safe to use with Deferred event behaviour).
    Looping = false
end)

while true do -- you could also do while (Looping and Value > 0) do and remove the condition check on the next line
    if Value <= 0 or not Looping then -- check the conditions
        break -- cancels the loop and resumes the code
    end
    -- your code here
    task.wait(1)
end

Yes, however the loop still will not break

Thanks everyone for the help I appreciate it!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.