Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is animation for tool still playing even when tool are unequipped?

I've been trying to make an animation for the player when they drink a cup of coffee, but the problem is when the player unequips the cup, the animations for the player are still playing until they are finished. None of my animations are looped. Is there any way to prevent this from happening, any bits of help will be appreciated. Thanks!

local tool = script.Parent.Parent
local player = game.Players.LocalPlayer
local character = player.Character
local drinkAnimation = script.Parent:WaitForChild("drinkingAnimation") 
local idleAnimation = script.Parent:WaitForChild("idleAnimation")
local returnAnimation = script.Parent:WaitForChild("returnAnimation")
local drinkAnimationTrack = character.Humanoid:LoadAnimation(drinkAnimation)
local idleAnimationTrack = character.Humanoid:LoadAnimation(idleAnimation)
local returnAnimationTrack = character.Humanoid:LoadAnimation(returnAnimation)
local isDrinking = false

tool.Activated:Connect(function()
	if tool.coffeeWater.Transparency ~= 0 then
		return
	end
	if isDrinking == false then 
		isDrinking = true
		drinkAnimationTrack:Play()
		wait(drinkAnimationTrack.Length)
		idleAnimationTrack:Play()
		wait(idleAnimationTrack.Length + 2)
		idleAnimationTrack:Stop()
		returnAnimationTrack:Play()
		wait(returnAnimationTrack.Length)
		returnAnimationTrack:Stop()
		isDrinking = false
	end	
end)		



tool.Unequipped:Connect(function() 
	drinkAnimationTrack:Stop()
	idleAnimationTrack:Stop()
	returnAnimationTrack:Stop()
end)

1 answer

✓ Accepted answer

Hi there! its because when you unequip a tool it doesn't stop all code in the activated function meaning if you unequip and there's a wait statement and after that it plays an animation that animation will play

to fix this simply check if the tool is still equipped after each wait statement

Snippet:

If Tool.Equipped then
 --Code
end

Hope this helped.

1 comment

0

Hi there, thanks for the reply. At first glance it seems to works fine, but when the player unequipped at the wait statement out side the if statement. Then him/she equipped the cup again, it will think equipped equals true and execute the next animation!

This discussion is preserved as it was. New answers, comments, and votes are closed.