How to destroy animation?

I made a script that gives a player a custom walk animation in a certain part of the game, afterwards, when the player touches a part, the custom walk animation gets destroyed.
However, the script isn’t working and the animation still plays whenever the player walks. Am I doing something wrong?

custom walk animation script

workspace.Checkpoints["26"].Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent == script.Parent and not gb2 then
		gb2 = true

		local walkanim = script:WaitForChild("Run")
		local walkanimtrack = humanoid.Animator:LoadAnimation(walkanim)

		humanoid.Running:Connect(function(speed)
			if speed > 0 then
				if not walkanimtrack.IsPlaying then
					walkanimtrack:Play()
				end
			else
				if walkanimtrack.IsPlaying then
					walkanimtrack:Stop()
				end
			end
		end)
		workspace.FunctionParts.FunctionPart15.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent == script.Parent and not gb3 then
				gb3 = true
				walkanimtrack:Destroy()
				wait()
			end
		end)
	end
end)

You can’t destroy an anim track you can only stop it

1 Like

Do you know any solution to this?

1 Like
for _,Animation in pairs (Humanoid:GetPlayingAnimationTracks()) do
	Animation:Stop()
end

Keep in mind this will stop ALL animations but you could probably check the animation names.