How would I make a function break a function that it's in?

Kind of a weird question, I want a function to break when a condition becomes true. This is what I was doing:

function foo()
    -- should break the function whenever the condition becomes true
    spawn(function()
        while wait() do
            if condition then
                return
            end
        end
    end)
    -- Stuff the function actually does
end

That return would only break the spawn function though, it wouldn’t break foo. Is there something kind of like this that you can do?

function foo()
    -- should break the function whenever the condition becomes true
    spawn(function()
        while wait() do
            if condition then
                foo:break()
            end
        end
    end)
    -- Stuff the function actually does
end

(Obviously that wouldn’t be the exact syntax, just trying to show what I want.)

You might want to look a little more into coroutines. You can yield, stop, and resume them when you want.

1 Like

I actually learned about coroutines, I just completely forgot about them. Thanks for reminding me