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

Tool won't stop playing animation upon unequipping?

Asked by 3 years ago

Hi, I'm having an issue with my script. So the script works entirely fine, besides the final part. To sort of explain the script, its designed to where when you equip the gun, it plays the draw animation, immediately followed by the LOOPED stance animation called Idle. When you fire the gun, it plays the fire animation. However, when you unequip the gun, the idle animation continues to play, and I can't get it to stop. Please let me know if you have a solution!

local Tool = script.Parent
local fire = Tool.Fire
local draw = Tool.Draw
local idle = Tool.Idle

local AnimateValue = script.Parent.AnimateValue

Tool.Equipped:connect(function()
    AnimateValue.Value = true
    local Character = Tool.Parent
    local Humanoid = Character.Humanoid

    local AnimationTrack = Humanoid:LoadAnimation(draw)
    local AnimationTrack1 = Humanoid:LoadAnimation(idle)
    if AnimateValue.Value == true then
        AnimationTrack:Play()
        wait(.5)
        AnimationTrack1:Play()
    else
        if AnimationTrack1.IsPlaying then
            AnimationTrack1:Stop()
        end
    end

end)


Tool.Unequipped:connect(function()
    AnimateValue.Value = false

end)



Tool.Activated:Connect(function()
    local Character = Tool.Parent
    local Humanoid = Character.Humanoid

    local AnimationTrack = Humanoid:LoadAnimation(fire)
    AnimationTrack:Play()
end)

2 answers

Log in to vote
1
Answered by
Roger111 347 Moderation Voter
3 years ago
Edited 3 years ago

Hi, so a better way to go about this and might as well solve your problem is by adding a variable that tracks the current animation. Then when it is time to stop the animations you will specifically call :Stop() on the animation that is currently playing.

Like so:

local Tool = script.Parent
local fire = Tool.Fire
local draw = Tool.Draw
local idle = Tool.Idle

    local currentAnim -- Here is where we add the variable

local AnimateValue = script.Parent.AnimateValue

Tool.Equipped:connect(function()
    AnimateValue.Value = true
    local Character = Tool.Parent
    local Humanoid = Character.Humanoid

    local AnimationTrack = Humanoid:LoadAnimation(draw)
    local AnimationTrack1 = Humanoid:LoadAnimation(idle)
    if AnimateValue.Value == true then
    currentAnim = AnimationTrack -- set the current animation
        AnimationTrack:Play()
        wait(.5)
    currentAnim:Stop() -- stop the previous animation for new one
    currentAnim = AnimationTrack1 -- set the current animation
        AnimationTrack1:Play()
    else
        if AnimationTrack1.IsPlaying then
            AnimationTrack1:Stop()
        end
    end

end)


Tool.Unequipped:connect(function()
    AnimateValue.Value = false
    currentAnim:Stop() -- stop the current animation on unequip
end)



Tool.Activated:Connect(function()
    local Character = Tool.Parent
    local Humanoid = Character.Humanoid

    local AnimationTrack = Humanoid:LoadAnimation(fire)
    AnimationTrack:Play()
end)
1
Thank you so much! alexduskthorn 40 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I Think The Problem Is That Your Animation Is LOOPING, You Can UnLoop The Animation By Going InTo The Animation And Turning Off the LOOPING.

Answer this question