Animation.Ended isn't working

Hello developers!

I’m currently trying to achieve an item that plays an animation and restore up an value when players uses it. But, for some reason, the function on Animation.Ended isn’t running

local debounce = false

script.Parent.Activated:Connect(function()
	if debounce == false then
		debounce = true	
		local anim = script.Animation
		local track = script.Parent.Parent:FindFirstChildWhichIsA("Humanoid"):FindFirstChildWhichIsA("Animator"):LoadAnimation(anim)
		script.Parent.BottleBottom["Metal Drinking Fountain 14 (SFX)"]:Play()
		track:Play()
		track.Ended:Connect(function()
			print("ksmaks")
			local plr = game.Players.LocalPlayer
			local sanity = plr:WaitForChild("Sanity")
			if sanity.Value >= 60 then
				sanity.Value = 100
				print("Ksksksks sanity1 ")
			else
				print("Sanity +40")
				sanity.Value += 40
			end

			task.wait(.1)
			print("Destroyed")
			script.Parent:Destroy()
		end)
	end

end)

Have you tried using track.Stopped?

track.Stopped:Connect(function()

end)

That might solve it

1 Like

Thanks. But I want to know, why .Ended dindn’t worked?

Based off the documentation here, AnimationTrack.Stopped works whenever the animation ended by it’s own accord or was stopped explicitly by the developer, not quite sure what’s the difference between those two is, but you can read it yourself (lazy).

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