If i use KeyFrameReached, and while the script is running, the animations gets stopped, will the script yield at the keyframe reached function?

nothing more than what the title says, i want to know this because this happened to me some time ago with getmarkerreachedsignal, and now i want to try keyframe reached.

forgot to add to the title, if the script is running and the animation gets stopped BEFORE the certain keyframe was reached

1 Like

Yes, it will, and it is going to be resumed the next time it is reached.

Example:

-- Suppose this animation is longer than 1 second
task.spawn(function()
	animation:Play()
	animation.KeyframeReached:Wait() -- yield
	print("Reached when played again")
end)

task.wait(1); print("Stopping")
animation:Stop()
task.wait(1); print("Playing again")
animation:Play()

If this poses a problem, I have few solutions for canceling on top of my head:

  1. Closing coroutines/cancelling threads
local thread = task.spawn(function()
	animation:Play()
	animation.KeyframeReached:Wait()
	print("Reached")
end)

task.wait(1); print("Stopping")
animation:Stop()
task.cancel(thread)
  1. Promises (roblox-lua-promise by evaera) - relatively similar to point 1

  2. Disconnecting connections

local connection = animation.KeyframeReached:Once(function(keyframe)
	print("Reached")
end)

animation:Play()
task.wait(1); print("Stopping")
animation:Stop()
connection:Disconnect(); connection = nil

im not really familiar with threads and task.spawn, is it possible to have keyframereached:connect(functiion) inside one of those?

I see you’ve marked the solution already, but just in case, sure, exactly like in the point 3, except :Once() is replaced by :Connect().

Don’t worry, coroutines are really not complicated, in the above cases they just wrap code and cancel the executed function later, so it doesn’t reach the end. Even the functions called in :Once() or :Connect() are ran in their own threads.

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