Why is my animation not stopping?


I have no idea why it’s not stopping.
This is the local script placed inside of the tool.
Please help me with this, or show me better/more effecient ways to change the animations for when a tool is equipped.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local WeaponConfig = require(ReplicatedStorage.Modules.Configs:WaitForChild("WeaponConfig"))
local Remotes = ReplicatedStorage:WaitForChild("Remotes").Events
local PlayAnimationRemote = Remotes:WaitForChild("PlayAnimation")
local HitboxRemote = Remotes:WaitForChild("Hitbox")

local tool = script.Parent
local player = Players.LocalPlayer

local weaponData = WeaponConfig[tool.Name]
if not weaponData then return end

local toolType = tool:GetAttribute("Type")
local debounceTime = weaponData.DebounceDuration
local debounce = false
local ActiveTracks = {}
local moving = false
local updateConnection
local attacking = false

local function playAnim(char, id, name, priority)
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end
	local animator = humanoid:FindFirstChildOfClass("Animator")
	if not animator then
		animator = Instance.new("Animator")
		animator.Parent = humanoid
	end
	if ActiveTracks[name] then
		ActiveTracks[name]:Stop()
		ActiveTracks[name]:Destroy()
	end
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://" .. tostring(id)
	local track = animator:LoadAnimation(anim)
	track.Priority = priority or Enum.AnimationPriority.Movement
	track.Name = name
	track:Play()
	ActiveTracks[name] = track
	return track
end

local function stopAnim(name)
	if ActiveTracks[name] then
		ActiveTracks[name]:Stop()
		ActiveTracks[name]:Destroy()
		ActiveTracks[name] = nil
	end
end

local function stopAllAnims()
	for name, _ in pairs(ActiveTracks) do
		stopAnim(name)
	end
end

local function updateMovementAnim()
	if attacking then return end
	local char = player.Character
	if not char then return end
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end
	local speed = humanoid.MoveDirection.Magnitude
	if speed > 0.1 then
		if not moving then
			moving = true
			if tool.Parent == char then
				playAnim(char, weaponData.Animations.RunWithSword, "Move", Enum.AnimationPriority.Movement)
			else
				playAnim(char, weaponData.Animations.RunWithoutSword, "Move", Enum.AnimationPriority.Movement)
			end
		end
	else
		if moving then
			moving = false
			if tool.Parent == char then
				playAnim(char, weaponData.Animations.IdleWithSword, "Idle", Enum.AnimationPriority.Idle)
			else
				playAnim(char, weaponData.Animations.IdleWithoutSword, "Idle", Enum.AnimationPriority.Idle)
			end
		end
	end
end

tool.Equipped:Connect(function()
	local char = player.Character or player.CharacterAdded:Wait()
	moving = false
	updateMovementAnim()
	updateConnection = RunService.RenderStepped:Connect(updateMovementAnim)
end)

tool.Unequipped:Connect(function()
	stopAllAnims()
	if updateConnection then
		updateConnection:Disconnect()
		updateConnection = nil
	end
	moving = false
	attacking = false
end)

tool.Activated:Connect(function()
	if debounce then return end
	debounce = true
	task.delay(debounceTime, function()
		debounce = false
	end)
	local char = player.Character or player.CharacterAdded:Wait()
	attacking = true
	PlayAnimationRemote:FireServer(tool, toolType)
	task.delay(weaponData.DebounceDuration or 0.5, function()
		attacking = false
	end)
end)

Any help is appreciated!

when you upload the animation, did you make sure the “loop” or “looped” boolean property was set to false. You can also just reupload the animation and overwrite the save with the loop turned off, that way you dont have to change the id

It was set to true, I turned it off and overwrote it, but now it just glitches.

It seems like your update anim function is only being called once, maybe you could have a function that loops via runservice to check if the player is moving, and then if he isnt, then cancel the anim. OTherwise continue and relay anim.
An example(not exact):

use runservice to check every frame if the player is moving. Then check if the player anim is playing. if not hen play the anim, if the anim i splaying than cancel out. And if the player is not moving and the anim is playing, than cancel the anim. this should eliminate having to use muplitple differnt functions and shoul dbe more modular

Would you be so kind to add that to my script? I kind of have a hard time understand stuff since I’m relatively new to scripting. Or could you make an example for me to implement?

You are trying to stop the animation by setting its animation priority to Enum.AnimationPriority.Idle instead of actually stopping it

How would I go about fixing it?