Animations not halting upon unequip

Hello, I’ve tried many times to resolve this bug in my script but have been unable to get the animations to stop when being unequipped, I’ve tried multiple solutions and looked on the DevForum but nothing has fixed this

Code context:
active = true: start idle animation
active = “atk”: start attacking seq
active = other: assume unequipped

Offending code:

local module = {}

local TweenService = game:GetService("TweenService")
local DebrisService = game:GetService("Debris")
function tween(timesec, EasingStyle, EasingDirection, PART, GOALS)
	local ti = TweenInfo.new(timesec, EasingStyle, EasingDirection)
	local tween = TweenService:Create(PART, ti, GOALS)
	tween:Play()
end

local playerdebounce = {}

function module.tool(ply, active, obj)
	local tool = game.Workspace:FindFirstChild(ply.Name):FindFirstChild("Sledgehammer")
	local humanoidinstance = game.Workspace:FindFirstChild(ply.Name):FindFirstChildWhichIsA("Humanoid")
	
	local animator = humanoidinstance:WaitForChild("Animator")
	local attackbuffer = Instance.new("Animation")
	local idlebuffer = Instance.new("Animation")
	attackbuffer.AnimationId = "rbxassetid://8887906371"
	local attackanimation = animator:LoadAnimation(attackbuffer)
	idlebuffer.AnimationId = "rbxassetid://8887914210"
	local idleanimation = animator:LoadAnimation(idlebuffer)
	
	if table.find(playerdebounce, ply.Name) == nil then
		idleanimation:Play()
	end
		
	print(active)
	
	if active == true then
		idleanimation.Priority = Enum.AnimationPriority.Idle
		idleanimation.Looped = true
		idleanimation:Play()
	elseif active == "atk" then
		if table.find(playerdebounce, ply.Name) ~= nil then
			return
		end
		
		table.insert(playerdebounce, ply.Name)
		spawn(function()
			task.wait(3.6)
			for _, v in pairs(playerdebounce) do
				if v == ply.Name then
					table.remove(playerdebounce, _)
				end
			end
		end)
		
		attackanimation.Priority = Enum.AnimationPriority.Action
		attackanimation.Looped = false
		attackanimation:Play()
		
		tool.Handle.Trail.Enabled = true
		task.wait(0.2)
		tool.Handle.Attack.PitchShiftSoundEffect.Octave = Random.new():NextNumber(0.98, 1.35)
		tool.Handle.Attack:Play()
		task.wait(0.35)
		tool.Handle.Trail.Enabled = false
	else
		print("Stopping niamtions dasiokjkla fd PLEASE JUST WORK ")
		attackanimation:Stop()
		idleanimation:Stop()
		attackbuffer:Destroy()
		idlebuffer:Destroy()
	end
end

return module

Please pardon the code for being messy, I’ve been trying a lot of things to fix this

Video of issue:

The code runs without errors or warnings

1 Like

Use the function Animator:GetPlayingAnimationTracks which returns an array with the currently playing AnimationTracks and then you can loop through the table and call AnimationTrack:Stop on each AnimationTrack

1 Like