AnimationTrack:Stop() not stopping

Hey, I’ve encountered an issue while trying to stop an idle animation, I’m using a ModuleScript as the main code for my katana, and when the unequip event is fired I would need to manually stop the animation, but that doesn’t work correctly and I would require some help.

The code is written below.

 local module = {}

function foundkatanatool(char)
	wait(.1)
	local tool = char:FindFirstChildWhichIsA("Tool")
	if tool then
		local typ = tool:FindFirstChild("Type")
		if typ and typ.ClassName == "StringValue" and typ.Value == "Sword" then
			return true
		end
	end
end
module.EquipNichirin = function(character)
	local model = character:FindFirstChild("SwordModel")
	if model and model:IsA("Model") and character:FindFirstChild("Humanoid") then
		local anim = character.Humanoid:LoadAnimation(script.Idle)
		anim:Play()
		
		if character.EquippedKatana.Value == false then
			character.HumanoidRootPart.UnSheathe:Play()
			local unsheatheanim = character.Humanoid:LoadAnimation(script.UnSheathe)
			unsheatheanim:Play()
			unsheatheanim:AdjustSpeed(.5)
		end
		
		character.EquippedKatana.Value = true
		
		local weld = model.Handle:FindFirstChild("HandleWeld")
		if weld then
		weld.Part0 = character:FindFirstChild("Right Arm")
		weld.C0 = CFrame.new(0, -1, 0) * CFrame.Angles(math.rad(270),math.rad(180),math.rad(90)) * CFrame.Angles(0, math.rad(90), 0)
		end
	end
end

module.UnequipNichirin = function(character)
	local model = character:FindFirstChild("SwordModel")
	if model and model:IsA("Model") and not foundkatanatool(character) and character:FindFirstChild("Humanoid") then
		local anim = character.Humanoid:LoadAnimation(script.Idle)
		wait()
		anim:Stop()

		character.HumanoidRootPart.Sheathe:Play()
		character.EquippedKatana.Value = false
		local weld = model.Handle:FindFirstChild("HandleWeld")
		if weld then
		delay(.12,function()
			if not foundkatanatool(character) then
		weld.Part0 = character:WaitForChild("Torso")
		weld.C0 = CFrame.new(-1, -.1, -1.2) * CFrame.Angles(math.rad(200),math.rad(180),math.rad(90))
			end
		end)
		end
	end
end
2 Likes

I don’t really see why it wouldn’t work, try putting a wait() in between the animation variable and anim:Stop()

i don’t see a reason why you stop a new animation that’s not played yet

1 Like

You’ve created another AnimationTrack that’s separate from the one playing by calling LoadAnimation. Try referencing all of the tracks somewhere at the start of the script so you can use the same ones.

It has started playing, I edited my post so you are able to see the whole script. Maybe that’ll be more useful.

I’ve tried putting wait() between but I just get the same result.

@Mex_Z I was just about to say this.
This may be your solution as you’re referencing the same animation in two different variables, which causes complications.

That’s not good: each time you equip or unequip you are making a new AnimationTrack by using the :LoadAnimation() method on your Humanoid. The thing is, first, you’ll reach a max loaded animation limit on your humanoid (I think the max is 256); and secondly, you don’t access the same AnimationTrack that was loaded on your Humanoid previously, so stoping that newly created AnimationTrack won’t stop the previously created AnimationTrack, if you see what I mean.

You can fix this by only loading every AnimationTracks once somewhere, and calling :Play() or :Stop() whenever you need to. (Which means you’ll need to store a reference to the AnimationTracks you’ve created somewhere)

2 Likes