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

Tool won't stop playing animation upon unequipping?

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

✓ Accepted answer

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 comment

1

Thank you so much!

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.

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