How to Handle AnimationTrack Yielding Indefinitely When Player Leaves or Resets

I have a script that plays an animation on a player and runs code after the animation ends. However this script yields indefinitely when the player leaves or resets while the animation is playing. What is the best way to solve this?

local animationTrack: AnimationTrack = -- some path

animationTrack:Play()

animationTrack.Ended:Wait() -- waits forever if player leaves or resets while animation is playing

-- code to be run after animation ends.

Im unsure how i could test this but maybe it could work

local player:Player = -- path to player
local animationTrack: AnimationTrack = -- some path

animationTrack:Play()

local ran_code = false

local yield = task.spawn(function()
	animationTrack.Ended:Wait()
	-- code after anim ended
	ran_code = true
end)

local resetCon,leaveCon

resetCon = player.Character:FindFirstChild("Humanoid").Died:Once(function()
	if leaveCon then leaveCon:Disconnect() end
	if not ran_code then -- making sure code doesnt run twice
		task.cancel(yield)
		-- code after anim ended (or specificly for resetting)
	end
end)

leaveCon = player.Destroying:Once(function()
	if resetCon then resetCon:Disconnect() end
	if not ran_code then -- making sure code doesnt run twice
		task.cancel(yield)
		-- code after anim ended (or specificly for leaving)
	end
end)

Its because animationTrack.Ended:Wait() waits until the animationTrack.Ended event is fired but the player dies or leaves it wont fire because the animator does not exist anymore. Instead use task.wait(animationTrack.Length)

Thank you for the reply. Your solution makes sense, but it does not guarantee that the code will resume running when the animation ends because of latency. However, it is definitely the most simplistic approach and I will consider this.

Thanks for your reply. This solution makes sense as well; it should work with precision. My only problem with this solution is that it is very long and tedious for something that shouldn’t be that complicated.

is there a reason you’re using :Wait() and not :Connect() or :Once()?
you should just listen to one of those signals and if anything happens cleanup manually to not cause a memory leak

Using :Connect() or :Once will still result in the same problem as events tied to the animationTrack will not fire when the player resets or leaves the game.

that’s why you can cleanup manually, you can have the same function run both when the animation ends and when the player leaves.

If I am understanding you correctly, you are suggesting the same solution as this?

Oh I believe the difference is that animationTrack.Ended:Connect() can be used instead of animationTrack.Ended:Wait(), eliminating the need to create a new thread using task.spawn()?

sort of, though this would be a lot simpler than bothering with threads.
This assumes you’d be having a cleanup table separate for each character/player. If your system is big enough then you can also just use a Maid/Janitor module instead.

-- | Variables
local cleanupTable = {}

-- | Functions
local function somethingOnEnd(index: number)
	print("yea")
	table.remove(cleanupTable, index)
end

local function clearCleanupTable()
	for _, value in cleanupTable do
		value[1]:Disconnect()
		value[2]()
	end
	cleanupTable = nil
end

-- | Code
local AnimationTrack = nil :: AnimationTrack
local Player = nil :: Player

local index = #cleanupTable + 1
local cleanup = AnimationTrack.Ended:Once(function()
	somethingOnEnd()
	table.remove(cleanupTable, index)
end)
table.insert(cleanupTable, index, { cleanup, somethingOnEnd })

-- ... on player leave
Player.Character.Destroying:Connect(function()
	clearCleanupTable()
end)

Thank you this is probably the best way to go. The method I posted earlier was completely pointless, sorry about that. Thanks for the help!