diff --git a/README.md b/README.md index b8b7759..b6fcae8 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ spr.completed(obj: Instance, callback: ()->()) Registers a callback function that will be called the next time the instance stops animating. The callback is only called once. This is useful for tracking instance lifetime, such as destroying a part when it becomes invisible. +To cancel the callback function, use the function returned from spr.completed, as given in the example below. + ### `spr.stop` ```lua spr.stop(obj: Instance, property: string?) @@ -127,6 +129,16 @@ spr.target(workspace.Part, 1, 1, {Transparency = 1}) spr.completed(workspace.Part, function() workspace.Part:Destroy() end) ``` +```lua +spr.target(script.Parent.TextButton, 1, 1, {BackgroundTransparency = 1}) +local cancel = spr.completed(script.Parent.TextButton, function() script.Parent.TextButton:Destroy() end) +wait(1) +-- if the background transparency has returned to the initial value (0) by another interaction, cancel the callback +if script.Parent.TextButton.BackgroundTransparency == 0 then + cancel() +end +``` + ### `spr.stop` ```lua diff --git a/spr.lua b/spr.lua index a0a124f..e148840 100644 --- a/spr.lua +++ b/spr.lua @@ -779,6 +779,18 @@ function spr.completed(instance: Instance, callback: ()->()) else completedCallbacks[instance] = {callback} end + + return function() + local callbackList2 = completedCallbacks[instance] + local index = table.find(callbackList2, callback) + if callbackList2 and index then + if #callbackList2 == 1 then + completedCallbacks[instance] = nil + else + table.remove(callbackList2, index) + end + end + end end return table.freeze(spr)