Animation Script Bug

So I was making a script that when you equip the tool :

  • Plays Idle Animation when idling
  • Plays Walking Animation when walking

The problem is that when I unequip the tool, the animations are still playing.
You can test the script by yourself to see how to looks in-game.

Code
-- Variables

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Tool = script.Parent
local Equipped = script.Parent:WaitForChild("Equipped_")

local IdleAnimationID = 9969328857
local IdleAnimation = Instance.new("Animation")
IdleAnimation.AnimationId = "rbxassetid://"..IdleAnimationID
local IdleAnimationTrack = Player.Character:WaitForChild("Humanoid"):LoadAnimation(IdleAnimation)
local IdleAnimationSpeed = 0.25

local WalkAnimationID = 9969492200
local WalkAnimation = Instance.new("Animation")
WalkAnimation.AnimationId = "rbxassetid://"..WalkAnimationID
local WalkAnimationTrack = Player.Character:WaitForChild("Humanoid"):LoadAnimation(WalkAnimation)

----------------------------------------------------------------

-- Code

local function PlayIdle()
	if IdleAnimationTrack then
		IdleAnimationTrack.Priority = Enum.AnimationPriority.Action
		IdleAnimationTrack.Looped = true
		IdleAnimationTrack:Play()
		IdleAnimationTrack:AdjustSpeed(IdleAnimationSpeed)
	end
end

local function StopIdle()
	if IdleAnimationTrack then
		IdleAnimationTrack:Stop()
	end
end

local function StopWalk()
	if WalkAnimationTrack then
		WalkAnimationTrack:Stop()
	end
end

local function PlayWalk()
	if WalkAnimationTrack then
		WalkAnimationTrack.Priority = Enum.AnimationPriority.Movement
		WalkAnimationTrack.Looped = true
		WalkAnimationTrack:Play()
	end
end

local function StopAllAnimations()
	wait(0.1)
	if IdleAnimationTrack and WalkAnimationTrack then
		IdleAnimationTrack:Stop()
		WalkAnimationTrack:Stop()
	end
end

local function WalkHandler()
	while wait() do
		if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
			StopIdle()
			PlayWalk()
		end
		if Humanoid.MoveDirection == Vector3.new(0,0,0) then
			PlayIdle()
			StopWalk()
		end
		if Equipped.Value == false then break end
	end
end

Tool.Equipped:Connect(WalkHandler)

Tool.Unequipped:Connect(StopIdle)
Tool.Unequipped:Connect(StopWalk)
Tool.Unequipped:Connect(StopAllAnimations)

Thanks for your help!

1 Like
  1. better do
Tool.Unequipped:Connect(function()
   StopIdle()
   StopWalk()
   StopAllAnimations()
end)
  1. There is a endless while loop, you should break it then tool is unequipped
while true do
if not equipped then break end
end
2 Likes

I don’t recommend use While wait() do Use RunService
So Like this

RunService.RendStepped:Connect(function() 
if not equipped then 
-- Stop the Animation
end
end) 

Or You could do like this

YourTool.Unequipped:Connect(function()
-- Stop the Animation
end)
1 Like

Thanks for your help.
It now works, but it still doesn’t stop the animations when unequipped.
Any ideas why?


Current script :

local function PlayIdle()
	if IdleAnimationTrack then
		IdleAnimationTrack.Priority = Enum.AnimationPriority.Action
		IdleAnimationTrack.Looped = true
		IdleAnimationTrack:Play()
		IdleAnimationTrack:AdjustSpeed(IdleAnimationSpeed)
	end
end

local function StopIdle()
	if IdleAnimationTrack then
		IdleAnimationTrack:Stop()
	end
end

local function StopWalk()
	if WalkAnimationTrack then
		WalkAnimationTrack:Stop()
	end
end

local function PlayWalk()
	if WalkAnimationTrack then
		WalkAnimationTrack.Priority = Enum.AnimationPriority.Movement
		WalkAnimationTrack.Looped = true
		WalkAnimationTrack:Play()
	end
end

local function WalkHandler()
	while wait() do
	if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
		StopIdle()
		PlayWalk()
	end
	if Humanoid.MoveDirection == Vector3.new(0,0,0) then
		PlayIdle()
		StopWalk()
	end
		while true do
			if Equipped.Value == false then break end
		end
	end
end

local function StopAllAnimations()
	wait(0.1)
	if IdleAnimationTrack and WalkAnimationTrack then
		IdleAnimationTrack:Stop()
		WalkAnimationTrack:Stop()
	end
end

Tool.Equipped:Connect(WalkHandler)

Tool.Unequipped:Connect(function()
	StopIdle()
	StopWalk()
	StopAllAnimations()
end)

you are making same mistake, you did the endless loop again.
please dont use

while true do

end

without understanding how it would work.

Anyways it should look like this

local function WalkHandler()
	while wait() do
	if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
		StopIdle()
		PlayWalk()
	end
	if Humanoid.MoveDirection == Vector3.new(0,0,0) then
		PlayIdle()
		StopWalk()
	end
	if Equipped.Value == false then break end
	end
end
1 Like

But this is my old code-

characterrr limiittttttttttttttt


Anyway it works but the walk only plays when idling and the idle plays only when walking

local Enumeration = Enum
local Script = script
local Tool = Script.Parent

local IdleAnimation = Instance.new("Animation")
IdleAnimation.AnimationId = "rbxassetid://9969328857"
local IdleTrack

local WalkAnimation = Instance.new("Animation")
WalkAnimation.AnimationId = "rbxassetid://9969492200"
local WalkTrack

local Connection

local function OnHumanoidMoveDirectionChanged(Humanoid, Override)
	local Magnitude = Humanoid.MoveDirection.Magnitude
	if Override then
		IdleTrack:Stop()
		WalkTrack:Stop()
	elseif Magnitude > 0 then
		IdleTrack:Stop()
		WalkTrack:Play()
	else
		WalkTrack:Stop()
		IdleTrack:Play()
		IdleTrack:AdjustSpeed(0.25)
	end
end

local function LoadAnimations()
	local Character = Tool.Parent
	if not Character then return end
	local Humanoid = Character:FindFirstChildOfClass("Humanoid")
	if (not Humanoid) or Humanoid.Health <= 0 then return end
	if not (IdleTrack and WalkTrack) then
		local Animator = Humanoid:FindFirstChildOfClass("Animator")
		if not Animator then return end
		IdleTrack = Animator:LoadAnimation(IdleAnimation)
		IdleTrack.Looped = true
		IdleTrack.Priority = Enumeration.AnimationPriority.Action
		WalkTrack = Animator:LoadAnimation(WalkAnimation)
		WalkTrack.Looped = true
		WalkTrack.Priority = Enumeration.AnimationPriority.Action
	end
	
	return Humanoid
end

local function OnEquipped()
	if Connection then return end
	local Humanoid = LoadAnimations()
	if not Humanoid then return end
	Connection = Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function() OnHumanoidMoveDirectionChanged(Humanoid) end)
end

local function OnUnequipped()
	if Connection then Connection:Disconnect() end
	OnHumanoidMoveDirectionChanged(nil, true)
end

Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequipped)