Animation playing after the tool was equipped


#1

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?

--Variables
wait()
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()
--Script:

--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,
	
	},
}
wait("loaded")
Tool.Equipped:Connect(function()
	print("Equipped Knife")
	Equipped = true
	PlayingAnimation = Animations.Equip
	PlayingAnimation.Animation:Play()
	StartTick = tick()
	repeat wait() until (tick() - StartTick) >= PlayingAnimation.Duration
	PlayingAnimation.Animation:Stop()
	PlayingAnimation = Animations.Idle
	PlayingAnimation.Animation:Play()
	canUse = true
	print("yes")
end)

Tool.Activated:Connect(function()
	if Equipped then
		if canUse then
			Activated = true
			Activetick = tick()
			PlayingAnimation.Animation:Stop()
			PlayingAnimation = Animations.Charge
			PlayingAnimation.Animation:Play()
			repeat wait() until (tick() - Activetick) >= .25 or Activated == false
			if Activated and  PlayingAnimation == Animations.Charge and canUse then
			PlayingAnimation.Animation:Stop()
			PlayingAnimation = Animations.ChargeHold
			PlayingAnimation.Animation:Play()
			end
		end
	end
end)



#2

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


#3

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


#4

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


#5

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


#6

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.


#7

the animation plays after I unequipped the knife


#8

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
        AnimTrack:Stop()
    end
end