Animation does not stop. How do I fix this

  1. What do you want to achieve? I want my animation to stop when it is unequipped

  2. What is the issue? My animation can play finely but I cant stop it anymore

  3. What solutions have you tried so far? I have seen code that I tried and thought it was a solution but it still did not work

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character
if not character or character.Parent then
	character = player.CharacterAdded:Wait()
end
local animation = tool:WaitForChild("Equip")
local animationTrack = character:WaitForChild("Humanoid"):LoadAnimation(animation)
local animationwalk = tool:WaitForChild("Walk")
local animationwalkTrack = character:WaitForChild("Humanoid"):LoadAnimation(animationwalk)

tool.Equipped:Connect(function()
	while true do
		if character.Humanoid.MoveDirection.Magnitude == 0 then
			animationTrack:Play()
			animationwalkTrack:Stop()
		else
			animationTrack:Stop()
			animationwalkTrack:Play()
		end
		wait(0.1)
	end


end)
tool.Unequipped:Connect(function()
	animationTrack:Stop()
	animationwalkTrack:Stop()
end)

Thanks in advanced!!!

1 Like

I think you can use while tool.Equipped do

Edit: or this:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character
if not character or character.Parent then
	character = player.CharacterAdded:Wait()
end
local animation = tool:WaitForChild("Equip")
local animationTrack = character:WaitForChild("Humanoid"):LoadAnimation(animation)
local animationwalk = tool:WaitForChild("Walk")
local animationwalkTrack = character:WaitForChild("Humanoid"):LoadAnimation(animationwalk)

local shouldPlay = false
tool.Equipped:Connect(function()
    shouldPlay = true
	while shouldPlay do
		if character.Humanoid.MoveDirection.Magnitude == 0 then
			animationTrack:Play()
			animationwalkTrack:Stop()
		else
			animationTrack:Stop()
			animationwalkTrack:Play()
		end
		wait(0.1)
	end


end)
tool.Unequipped:Connect(function()
        shouldPlay  = false
	animationTrack:Stop()
	animationwalkTrack:Stop()
end)
3 Likes

I think you can remove your while true do loop :

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character
if not character or character.Parent then
	character = player.CharacterAdded:Wait()
end
local animation = tool:WaitForChild("Equip")
local animationTrack = character:WaitForChild("Humanoid"):LoadAnimation(animation)
local animationwalk = tool:WaitForChild("Walk")
local animationwalkTrack = character:WaitForChild("Humanoid"):LoadAnimation(animationwalk)

tool.Equipped:Connect(function()
	if character.Humanoid.MoveDirection.Magnitude == 0 then
		animationTrack:Play()
		animationwalkTrack:Stop()
		animationTrack:Stop()
		animationwalkTrack:Play()
	end
end)
tool.Unequipped:Connect(function()
	animationTrack:Stop()
	animationwalkTrack:Stop()
end)

I hope that was helpful ! :wink:

2 Likes

Thanks so much. I can use this for a lot of my tools as it also doesn’t work for them. Thanksss!!!

3 Likes