Stopping an animation after a tool is unequipped

I have a script that plays an animation when you equip a Bloxy Cola
but I don’t know how to stop the animation after it’s unequipped I don’t know how to do this but I’ve tried one thing but it didn’t work.
This is the script:

local Tool = script.Parent;
local plr = script.Parent.Parent.Parent
local opened = script.Parent.opened

enabled = true




function onActivated()
	if not enabled  then
		return
	end
	if opened.Value == true then
		enabled = false


		Tool.Handle.DrinkSound:Play()
		Tool.Parent.Humanoid:LoadAnimation(Tool.drink):Play()
		task.wait(5)

		enabled = true

	end
end
enabled = true


function onEquipped()
	if opened.Value == false then
		Tool.Parent.Humanoid:LoadAnimation(Tool.equip):Play()
		Tool.Handle.OpenSound:play()
		task.wait(1)
		opened.Value = true
	end
end


script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)
local Tool = script.Parent
local plr = script.Parent.Parent.Parent
local opened = script.Parent.opened

enabled = true
track = nil

function onActivated()
	if not enabled  then
		return
	end
	if opened.Value == true then
		enabled = false


		Tool.Handle.DrinkSound:Play()
		Tool.Parent.Humanoid:LoadAnimation(Tool.drink):Play()
		task.wait(5)

		enabled = true

	end
end
enabled = true


function onEquipped()
	if opened.Value == false then
		if not track then track = Tool.Parent.Humanoid:LoadAnimation(Tool.equip):Play() end
		Tool.Handle.OpenSound:play()
		task.wait(1)
		opened.Value = true
	end
end

function onUnequipped()
	if track then track:Stop() end
end

script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)
script.Parent.Unequipped:connect(onUnequipped)
1 Like

Basic explanation @ASTROCPF ;

  • New event called Tool.Unequipped
  • Connect that to a function
  • And just put “Animation:Stop()”

event as in remote event or event as in function?
and how would I put that in the script

They were just explaining what I added to the script you provided.

Yes but your script didn’t work.

Took your script, combined with other replier’s scripts and furthermore modified it a myself, tested it in studio myself and everything worked perfectly for me. Scripts you received before this one should’ve also worked but try this anyway and let me know how it works for you.

-- Script
local tool = script.Parent
local player = tool.Parent.Parent
local opened = tool.opened

local enabled = true
local equipTrack -- 'equipTrack' - currently equal to 'nil' but will reference the 'equip' animation later on.
local drinkTrack -- 'drinkTrack' - like 'equipTrack', currently equal to 'nil' but will reference the 'drink' animation later on.

function onActivated()
	if not enabled then -- If 'enabled' variable is 'false', return.
		return
	end
	
	if opened.Value then -- If 'Value' property of 'opened' is 'true', set & play 'drink' animation.
		enabled = false
		
		tool.Handle.DrinkSound:Play()
		
		if drinkTrack == nil then
			drinkTrack = tool.Parent.Humanoid:LoadAnimation(tool.drink)
			drinkTrack:Play()
		else
			drinkTrack:Play()
		end
		
		task.wait(5)

		enabled = true
	end
end

function onEquipped()
	if not opened.Value then -- If 'Value' property of 'opened' is 'false', set & play 'equip' animation or just play 'equip' animation.
		tool.Handle.OpenSound:Play()
		
		if equipTrack == nil then
			equipTrack = tool.Parent.Humanoid:LoadAnimation(tool.equip)
			equipTrack:Play()
		else
			equipTrack:Play()
		end
		
		task.wait(1)
		opened.Value = true
	end
end

function onUnequipped()
	if drinkTrack ~= nil then -- If 'drinkTrack' variable isn't equal to 'nil', stop playing what it's equal to.
		drinkTrack:Stop()
	end
	
	if equipTrack ~= nil then -- If 'equipTrack' variable isn't equal to 'nil', stop playing what it's equal to.
		equipTrack:Stop()
	end
end


tool.Activated:connect(onActivated)
tool.Equipped:connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)
1 Like

THANK YOU SO MUCH

I say this in caps because I was desperate lol

I know it works because I tested it before providing it, although it seems you provided the wrong information in your original post.

but I don’t know how to stop the animation after it’s unequipped

You only mentioned trying to stop one track, not both tracks. The script I provided only stops the ‘equip’ track.