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!
Introducing: Yield Module (Cancellable “wait”)
Yield allows you to create a “yield” object using y = Yield.new(n, interval)
, and start or break it using y:Start()
or 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.
Here is an example script and its output using Yield:
Example Script
Script:
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)
Output:
Broken: false
Yielding…
Breaking the yield…
Broken: true
Yield finished! Lasted for: 2.01778639999975
You can get Yield module here.
Yield by @G_PL0X (windowschips).
You are free to do whatever you want with this script.