Sword walk/run animation buggy

Making when someone walks with the sword, animation plays.
Problem is, the animation always plays, and its kind of random if it plays or not.
I’ve looked around youtube for a bit of help.
Here’s my code

local animation = script:WaitForChild("Animations")
local idle = animation:WaitForChild("Idle")
local run = animation:WaitForChild("Run")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local Humanoid = char:WaitForChild("Humanoid")
local track = char.Humanoid:LoadAnimation(idle)
local animtrack = Humanoid:LoadAnimation(run)
track.Looped = true
local weld
local function handleWalkAnim()
	if Humanoid.MoveDirection ~= Vector3.new() and not animtrack.IsPlaying then
		animtrack:Play()
	elseif Humanoid.MoveDirection == Vector3.new() and animtrack.IsPlaying then
		animtrack:Stop()
	end
end
script.Parent.Equipped:Connect(function()
	track:Play()
	handleWalkAnim()
end)
script.Parent.Unequipped:Connect(function()
	track:Stop()
end)
game.ReplicatedStorage.Remotes.Jam.OnClientEvent:Connect(function()
	game.Workspace.Folder.Courage:Play()
end)

I forgot to mention earlier that handleAnim needs to be called every frame or connected to humanoid:GetPropertySignal:MoveDirection().

local RunService = game:GetService("RunService")

local renderStepConn

script.Parent.Equipped:Connect(function()
	track:Play()
	renderStepConn = RunService.RenderStepped:Connect(handleAnim)
end)
script.Parent.Unequipped:Connect(function()
	track:Stop()
        renderSteppedConn:Disconnect()
        renderSteppedConn = nil
end)
2 Likes

Weirdly, doesn’t work

local RunService = game:GetService("RunService")

local renderSteppedConn
local animation = script:WaitForChild("Animations")
local idle = animation:WaitForChild("Idle")
local run = animation:WaitForChild("Run")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local Humanoid = char:WaitForChild("Humanoid")
local track = char.Humanoid:LoadAnimation(idle)
local animtrack = Humanoid:LoadAnimation(run)
track.Looped = true
local weld
local function handleWalkAnim()
	if Humanoid.MoveDirection ~= Vector3.new() and not animtrack.IsPlaying then
		animtrack:Play()
	elseif Humanoid.MoveDirection == Vector3.new() and animtrack.IsPlaying then
		animtrack:Stop()
	end
end
script.Parent.Equipped:Connect(function()
	track:Play()
	renderSteppedConn = RunService.RenderStepped:Connect(handleWalkAnim)
	renderSteppedConn:Disconnect()
	renderSteppedConn = nil

end)
script.Parent.Unequipped:Connect(function()
	track:Stop()
end)
game.ReplicatedStorage.Remotes.Jam.OnClientEvent:Connect(function()
	game.Workspace.Folder.Courage:Play()
end)

EDIT: Nevermind! I had to move

	renderSteppedConn:Disconnect()
	renderSteppedConn = nil

to the unequipped function!