Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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

Log in to vote
0
Answered by
4jnx 50
3 years ago
Edited 3 years ago

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.

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! JackRB_Official 2 — 3y
Ad

Answer this question