Animation playing after the tool was equipped


I’m trying to make a knife for my new game but something went terribly wrong, the knife animations plays after I unequipped the knife, can anyone help fix this?

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local PlayingAnimation = nil

local Activated = false
local Equipped = false
local canUse = false

local StartTick = tick()
local Activetick = tick()

local Mouse = Player:GetMouse()

--repeat wait() until Character ~= nil

local Animations = {
	Charge = {
		Animation = Character.Humanoid:LoadAnimation(script.Parent:WaitForChild("Charge")),
		Duration = 1
	Idle = {
		Animation = Character.Humanoid:LoadAnimation(script.Parent:WaitForChild("Idle")),
	ChargeHold = {
		Animation = Character.Humanoid:LoadAnimation(script.Parent:WaitForChild("ChargeHold")),
	Stab1 = {
		Animation = Character.Humanoid:LoadAnimation(script.Parent:WaitForChild("Stab1")),
		Duration = .55
	Stab2 = {
		Animation = Character.Humanoid:LoadAnimation(script.Parent:WaitForChild("Stab2")),	
		Duration = .8
	Equip = {
		Animation = Character.Humanoid:LoadAnimation(script.Parent:WaitForChild("Equip")),	
		Duration = 1
	EquipSlow = {
		Animation = Character.Humanoid:LoadAnimation(script.Parent:WaitForChild("EquipSlow")),	
		Duration = 1,
	print("Equipped Knife")
	Equipped = true
	PlayingAnimation = Animations.Equip
	StartTick = tick()
	repeat wait() until (tick() - StartTick) >= PlayingAnimation.Duration
	PlayingAnimation = Animations.Idle
	canUse = true

	if Equipped then
		if canUse then
			Activated = true
			Activetick = tick()
			PlayingAnimation = Animations.Charge
			repeat wait() until (tick() - Activetick) >= .25 or Activated == false
			if Activated and  PlayingAnimation == Animations.Charge and canUse then
			PlayingAnimation = Animations.ChargeHold


Aren’t they suppose to play? Or do you mean the idle animation is playing too early?


Why are you using a string in the wait function? This might cause really long wait times and be your problem.


lmao ok but that’s not the reason why the animation play after I unequipped the knife


I don’t understand your issue, is the animation playing when you’ve unequipped or equipping?


The details of this thread are far too vague. What issue are you experiencing currently, what is the current result and how have you attempted to debug this yourself?

Posting a block of code and saying that “something went wrong” is not sufficient enough to begin looking into the problem. You should also do your own debugging, since these kinds of problems may be oversights on your behalf that you aren’t seeing.


the animation plays after I unequipped the knife


For starters, I had a skim through your code and saw no sign of having an Unequipped handler. There’s an event used in parallel to Tool.Equipped which is Tool.Unequipped. Have you attempted to use this yet?

In terms of stopping animation tracks, you have the option of calling Stop directly on the variables holding references to the AnimationTrack since those are present in your animations table. You can also run GetPlayingAnimationTracks on the Humanoid and call Stop on any of the array’s entries that meet your name criteria for being stopped.

for _, AnimTrack in pairs(runAboveFunction) do
    if AnimTrackNameCheckLine then