Ever wanted to use “task.wait” or “wait”, but you couldn’t cancel it, so you had to resort to hacky solutions using coroutines?
I have just the solution for you!
Yield allows you to create a “yield” object using
y = Yield.new(n, interval), and start or break it using
y:Break() respectively. You can check if a yield is broken using
y:IsBroken(), which returns a boolean.
A yield object’s broken state will be enabled by calling
:Break() on it, or by letting it finish. When a yield is broken, it cannot be broken or started again.
local yield = Yield.new(5) print("Broken:", yield:IsBroken()) task.delay(2, function() warn("Breaking the yield...") yield:Break() print("Broken:", yield:IsBroken()) end) print("Yielding...") local time = yield:Start() print("Yield finished! Lasted for:", time)
Breaking the yield…
Yield finished! Lasted for: 2.01778639999975