Tool animation issue with Animation.Stopped:Wait() & Tool.Unequipped function

Hello!
I have nearly finished a simple soda that the player will can use. When the soda Tool is equipped, it plays an animation. When the player clicks, the player “drinks” the content. Everything works fine. But…
I have seen that the player will be able to glitch himself with the drink animation. I use Animation.Stopped:Wait() to make the soda more realistic and play the different animations correctly.
Here is the Local script:

local CAN = script.Parent
local COOLDOWN = false
local DRINK_REMAINING = 5

CAN.Equipped:Connect(function(Mouse)
	local ANIMATION = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.HOLD)
	local ANIMATION_DRINK = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.DRINK)
	ANIMATION:Play()
	Mouse.Button1Down:Connect(function()
		if COOLDOWN == false then
			if DRINK_REMAINING > 0 then
				COOLDOWN = true
				ANIMATION:Stop()
				ANIMATION_DRINK:Play()
				script.Parent.Can.DRINK:Play()
				ANIMATION_DRINK.Stopped:Wait()
				ANIMATION:Play()
				DRINK_REMAINING = DRINK_REMAINING - 1
				if DRINK_REMAINING == 0 then
					ANIMATION:Stop()
					ANIMATION_DRINK:Stop()
					CAN:Destroy()
				end
				COOLDOWN = false
			end
		end
	end)
	CAN.Unequipped:Connect(function()
		ANIMATION:Stop()
		ANIMATION_DRINK:Stop()
	end)
end)

So, in the end, the only issue I saw is that the player will be able to unequip the tool while the drink animation is playing, and he will glitch himself with the animation because the script didn’t detect that he unequipped the tool, because the script is waiting for the drink animation to stop (I guess).
Is there any way to avoid that, please ?
Thank you!

If you make ANIMATION_DRINK a global variable then you can do

CAN.Unequipped:Connect(function()
	if DRINK_REMAINING then DRINK_REMAINING:Stop() end
	--(this just stops the drinking animation on unequip.)
end)
1 Like

Do you mean, if I put ANIMATION_DRINK = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.DRINK) as a global variable like DRINK_REMAINING, etc ?

do at the top Local ANIMATION_DRINK

then remove the local for the other one in the LoadAnimation thing.
then put the unequipped example earlier at the bottom

I tried to use what you said, so the script looked like this:

local CAN = script.Parent
local COOLDOWN = false
local DRINK_REMAINING = 5
local ANIMATION_DRINK
local ANIMATION_HOLD

CAN.Equipped:Connect(function(Mouse)
	ANIMATION_HOLD = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.HOLD)
	ANIMATION_DRINK = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.DRINK)
	ANIMATION_HOLD:Play()
	Mouse.Button1Down:Connect(function()
		if COOLDOWN == false then
			if DRINK_REMAINING > 0 then
				COOLDOWN = true
				ANIMATION_HOLD:Stop()
				ANIMATION_DRINK:Play()
				script.Parent.Can.DRINK:Play()
				ANIMATION_DRINK.Stopped:Wait()
				ANIMATION_HOLD:Play()
				DRINK_REMAINING = DRINK_REMAINING - 1
				if DRINK_REMAINING == 0 then
					ANIMATION_HOLD:Stop()
					ANIMATION_DRINK:Stop()
					CAN:Destroy()
				end
				COOLDOWN = false
			end
		end
	end)
end)

CAN.Unequipped:Connect(function()
	if ANIMATION_HOLD then
		ANIMATION_HOLD:Stop()
	end
	if ANIMATION_DRINK then
		ANIMATION_DRINK:Stop()
	end
end)

But the animation doesn’t seem to stop at all. Even the Hold one.

Alright. I figured it out.
I finally fixed the issue.

Solution:
I removed the “local” of ANIMATION_HOLD = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.HOLD) and ANIMATION_DRINK = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ANIMATIONS.DRINK), making them global and usable in the Unequipped function. (I removed local ANIMATION_DRINK and local ANIMATION_HOLD at the beginning.)
After that, I removed the line calling the ANIMATION_HOLD to stop. So I removed the linked line, calling play for this animation after the user played the drink animation.

And you’re done! I hope it helps.