Help with tool idle animation

So, I’m making a sword.
And seems like the sword idle is broken.

My attack script:

game.ReplicatedStorage.Attack.OnServerEvent:Connect(function(player, damage, tool, charge, swing, hitId, lightId, heavyId, attackType)
	
	local event
	playSound(swing, tool)
	local newHitboxRay = RaycastHitbox.new(tool.HitPart)
	newHitboxRay.RaycastParams = RaycastParams.new()
	newHitboxRay.RaycastParams.FilterDescendantsInstances = {player.Character}
	newHitboxRay.RaycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	newHitboxRay.Visualizer = false
	
	local lightAttack = Instance.new("Animation")
	lightAttack.Name = "LightAttack"
	lightAttack.Parent = game.Workspace
	lightAttack.AnimationId = lightId
	lightAttack = player.Character.Humanoid.Animator:LoadAnimation(lightAttack)
	
	local heavyAttack = Instance.new("Animation")
	heavyAttack.Name = "HeavyAttack"
	heavyAttack.Parent = game.Workspace
	heavyAttack.AnimationId = heavyId
	heavyAttack = player.Character.Humanoid.Animator:LoadAnimation(heavyAttack)
	
	local att0, att1 = tool.HitPart:FindFirstChild("Att0"), tool.HitPart:FindFirstChild("Att1")
	local trail = Instance.new("Trail")
	trail.Color = ColorSequence.new(Color3.new(255, 255, 255))
	trail.Attachment0 = att0
	trail.Attachment1 = att1
	trail.Lifetime = 0.1
	trail.Transparency = NumberSequence.new{
		NumberSequenceKeypoint.new(0.0, 0.6),
		NumberSequenceKeypoint.new(0.5, 0.85),
		NumberSequenceKeypoint.new(1.0, 1),
	}
	trail.Parent = tool.HitPart
	
	local chosen
	
	if attackType == "LightAttack" then
		chosen = lightAttack
	else
		chosen = heavyAttack
	end
	
	game.ReplicatedStorage.DecreaseStamina:Fire(player, charge) -- Firing bindable event to decrease player's stamina
	
	newHitboxRay.OnHit:Connect(function(hit, humanoid)
		print(hit)
		humanoid:TakeDamage(damage)
		playSound(hitId, tool)
		newHitboxRay:HitStop()
	end)
	
	newHitboxRay:HitStart()
	
	game.ReplicatedStorage.ChangeDebounce:Fire(player)
	
	chosen:Play()
	
	repeat wait() until chosen.IsPlaying == false
	
	newHitboxRay:HitStop()
	trail:Destroy()
end)

And boom!
This thing happens when I attack and then unequip the sword

Is there any solution?

1 Like

Tricky one. Maybe try waiting for a while then stopping the animation? Roblox does have a tendency to screw up animation executing and loading. I see you’re loading animations via the Animator Instance, that’s good practise.

From looking at this, it looks like ROBLOX is having a hard time playing your animation properly. Maybe set the animation priority to action and clear all other playing animations before playing it? Not really sure.

No, It happens when the tool is activated. And then unequipped it immediately. This thing happens

Based on the video, I’m not entirely sure what the problem is but here’s my answer.

When it comes time to play the idle animation after the swing, don’t just yet! Instead, check if the sword is still equipped. If it is, the idle animation can play. This should stop the idle animation from playing after the sword is unequipped.