Stop animation when unequip tool

local takePinOffAnim = script.removepin --Name Your Animation something like this

local function stopanim()
	local AnimationTracks = script.Parent.Parent.Humanoid:GetPlayingAnimationTracks()
	for i, track in pairs (AnimationTracks) do
		if track.Name == "removepin" then
			track:Stop()
		end
	end
end
function OnUnequipped()
	--MyPlayer = PlayersService:GetPlayerFromCharacter(Tool.Parent)
	local Players = game:GetService("Players")

	--local player = Players.LocalPlayer
	local character = script.Parent.Parent 
	local humanoid = character:WaitForChild("Humanoid")

	
	

	stopanim()


end
1 Like

nope, still not stopping the animation after playing…
here’s my WHOLE script

local GRAVITY_ACCELERATION = workspace.Gravity
local ammo = script.Parent.Ammo
local maxammo = script.Parent.MaxAmmo
local RELOAD_TIME = 0
local ROCKET_SPEED = 1
a = true
local tool = script.Parent
local animation = nil

local MISSILE_MESH_SCALE = Vector3.new(0.3,0.3,0.3)
local ROCKET_PART_SIZE = Vector3.new(0.1,0.1,1)
local DebrisService = game:GetService('Debris')
local PlayersService = game:GetService("Players")

local MyPlayer

local Tool = script.Parent
local ToolHandle = Tool:WaitForChild('Handle')


local MouseLoc = Tool:WaitForChild("MouseLoc",10)

local RocketScript = script:WaitForChild("Rocket")
local SwooshSound = script:WaitForChild("Swoosh")
local BoomSound = script:WaitForChild("Boom")


local Rocket = Instance.new("Part") do

	Rocket.Name = "Rocket"
	Rocket.FormFactor = Enum.FormFactor.Custom --NOTE: This must be done before changing Size
	Rocket.Size = ROCKET_PART_SIZE
	Rocket.CanCollide = false
	Rocket.Color = Color3.new(0.998901, 0.928023, 0.0595712)

	-- Add the mesh
	local mesh = Instance.new('SpecialMesh', Rocket)

	mesh.Scale = MISSILE_MESH_SCALE
	local a1 =  Instance.new('Attachment', Rocket)
	a1.Position = Vector3.new(0, 0.043, 0.001)
	local a2 =  Instance.new('Attachment', Rocket)
	a2.Position = Vector3.new(0, -0.035, -0.013)
	-- Add fire
	local fire = Instance.new('Trail', Rocket)
	fire.Attachment0 = a1
	fire.Attachment1 = a2
	fire.Lifetime = 1
	fire.MaxLength = 1

	-- Add a force to counteract gravity
	local bodyForce = Instance.new('BodyForce', Rocket)
	bodyForce.Name = 'Antigravity'
	bodyForce.Force = Vector3.new(0, Rocket:GetMass() * GRAVITY_ACCELERATION, 0)

	-- Clone the sounds and set Boom to PlayOnRemove
	local swooshSoundClone = SwooshSound:Clone()
	swooshSoundClone.Parent = Rocket
	local boomSoundClone = BoomSound:Clone()
	boomSoundClone.PlayOnRemove = true
	boomSoundClone.Parent = Rocket

	-- Attach creator tags to the rocket early on
	local creatorTag = Instance.new('ObjectValue', Rocket)
	creatorTag.Value = MyPlayer
	creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
	local iconTag = Instance.new('StringValue', creatorTag)
	iconTag.Value = Tool.TextureId
	iconTag.Name = 'icon'

	-- Finally, clone the rocket script and enable it
	local rocketScriptClone = RocketScript:Clone()
	rocketScriptClone.Parent = Rocket
	wait(1)
	rocketScriptClone.Disabled = false
end


local function OnActivated()
	if ammo.Value > 0 then
	local myModel = MyPlayer.Character
	if Tool.Enabled and myModel and myModel:FindFirstChildOfClass("Humanoid") and myModel.Humanoid.Health > 0 then
		Tool.Enabled = false
		local Pos = MouseLoc:InvokeClient(MyPlayer)

		local rocketClone = Rocket:Clone()
		DebrisService:AddItem(rocketClone, 30)
		rocketClone.BrickColor = BrickColor.Black()
		ammo.Value = ammo.Value - 1
		local spawnPosition = (ToolHandle.CFrame * CFrame.new(0, 0, 0.5)).p
		rocketClone.CFrame = CFrame.new(spawnPosition) --I CHANGED IT TO ONLY SPAWN POSITION, THIS WAS SPAWNPOSITION + POS,
		rocketClone.Velocity = Pos * ROCKET_SPEED --NOTE: This should be done before assigning Parent
		rocketClone.Parent = workspace
			rocketClone:SetNetworkOwner(nil)
		

		wait(0.25)

		Tool.Enabled = true
		end
	else
		if ammo.Value == 0 then
			if a then
				a = false
				wait(1)
				a = true
			ammo.Value = 10
				maxammo.Value = maxammo.Value - 10
				end
		end
	end
	
	
end
function OnEquipped()
	
	
	local MyPlayer = game.Players:GetPlayerFromCharacter(Tool.Parent)
	local character = MyPlayer.Character

	local animation = character:WaitForChild("Humanoid").Animator:LoadAnimation(script:WaitForChild("Animation"))
	animation:Play()


end
function OnUnequipped()
	--MyPlayer = PlayersService:GetPlayerFromCharacter(Tool.Parent)
	
	if animation ~= nil then
		animation:Stop()
	end
end
local uis = game:GetService("UserInputService");

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
		print("sus")--Also, could be written as [[inputObject.KeyCode == "R"]]
		if ammo.Value < 10 and a then
			a = false
			local x = 10 - ammo.Value
			wait(1)
			maxammo.Value = maxammo.Value - x
			ammo.Value = 10
		end
			    end
		end)

Tool.Unequipped:Connect(OnUnequipped)
Tool.Equipped:Connect(OnEquipped)
Tool.Activated:Connect(OnActivated)

Hey there! Could you remove the OnUnequipped function and in the OnEquipped function, at the end could you try adding

if Tool.Unequipped then
animation:Stop()

Hopefully that works!

now the animation don’t play at all… heres the error

  08:08:06.139  Players.foxnoobkite.Backpack.RocketLauncher.Server:81: attempt to index nil with 'Character'  -  Server - Server:81
  08:08:06.139  Stack Begin  -  Studio
  08:08:06.139  Script 'Players.foxnoobkite.Backpack.RocketLauncher.Server', Line 81 - function OnActivated  -  Studio - Server:81
  08:08:06.139  Stack End  -  Studio
local GRAVITY_ACCELERATION = workspace.Gravity
local ammo = script.Parent.Ammo
local maxammo = script.Parent.MaxAmmo
local RELOAD_TIME = 0
local ROCKET_SPEED = 1
a = true
local tool = script.Parent
local animation = nil

local MISSILE_MESH_SCALE = Vector3.new(0.3,0.3,0.3)
local ROCKET_PART_SIZE = Vector3.new(0.1,0.1,1)
local DebrisService = game:GetService('Debris')
local PlayersService = game:GetService("Players")

local MyPlayer

local Tool = script.Parent
local ToolHandle = Tool:WaitForChild('Handle')


local MouseLoc = Tool:WaitForChild("MouseLoc",10)

local RocketScript = script:WaitForChild("Rocket")
local SwooshSound = script:WaitForChild("Swoosh")
local BoomSound = script:WaitForChild("Boom")


local Rocket = Instance.new("Part") do

	Rocket.Name = "Rocket"
	Rocket.FormFactor = Enum.FormFactor.Custom --NOTE: This must be done before changing Size
	Rocket.Size = ROCKET_PART_SIZE
	Rocket.CanCollide = false
	Rocket.Color = Color3.new(0.998901, 0.928023, 0.0595712)

	-- Add the mesh
	local mesh = Instance.new('SpecialMesh', Rocket)

	mesh.Scale = MISSILE_MESH_SCALE
	local a1 =  Instance.new('Attachment', Rocket)
	a1.Position = Vector3.new(0, 0.043, 0.001)
	local a2 =  Instance.new('Attachment', Rocket)
	a2.Position = Vector3.new(0, -0.035, -0.013)
	-- Add fire
	local fire = Instance.new('Trail', Rocket)
	fire.Attachment0 = a1
	fire.Attachment1 = a2
	fire.Lifetime = 1
	fire.MaxLength = 1

	-- Add a force to counteract gravity
	local bodyForce = Instance.new('BodyForce', Rocket)
	bodyForce.Name = 'Antigravity'
	bodyForce.Force = Vector3.new(0, Rocket:GetMass() * GRAVITY_ACCELERATION, 0)

	-- Clone the sounds and set Boom to PlayOnRemove
	local swooshSoundClone = SwooshSound:Clone()
	swooshSoundClone.Parent = Rocket
	local boomSoundClone = BoomSound:Clone()
	boomSoundClone.PlayOnRemove = true
	boomSoundClone.Parent = Rocket

	-- Attach creator tags to the rocket early on
	local creatorTag = Instance.new('ObjectValue', Rocket)
	creatorTag.Value = MyPlayer
	creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
	local iconTag = Instance.new('StringValue', creatorTag)
	iconTag.Value = Tool.TextureId
	iconTag.Name = 'icon'

	-- Finally, clone the rocket script and enable it
	local rocketScriptClone = RocketScript:Clone()
	rocketScriptClone.Parent = Rocket
	wait(1)
	rocketScriptClone.Disabled = false
end


local function OnActivated()
	if ammo.Value > 0 then
	local myModel = MyPlayer.Character
	if Tool.Enabled and myModel and myModel:FindFirstChildOfClass("Humanoid") and myModel.Humanoid.Health > 0 then
		Tool.Enabled = false
		local Pos = MouseLoc:InvokeClient(MyPlayer)

		local rocketClone = Rocket:Clone()
		DebrisService:AddItem(rocketClone, 30)
		rocketClone.BrickColor = BrickColor.Black()
		ammo.Value = ammo.Value - 1
		local spawnPosition = (ToolHandle.CFrame * CFrame.new(0, 0, 0.5)).p
		rocketClone.CFrame = CFrame.new(spawnPosition) --I CHANGED IT TO ONLY SPAWN POSITION, THIS WAS SPAWNPOSITION + POS,
		rocketClone.Velocity = Pos * ROCKET_SPEED --NOTE: This should be done before assigning Parent
		rocketClone.Parent = workspace
			rocketClone:SetNetworkOwner(nil)
		

		wait(0.25)

		Tool.Enabled = true
		end
	else
		if ammo.Value == 0 then
			if a then
				a = false
				wait(1)
				a = true
			ammo.Value = 10
				maxammo.Value = maxammo.Value - 10
				end
		end
	end
	
	
end
function OnEquipped()
	
	
	local MyPlayer = game.Players:GetPlayerFromCharacter(Tool.Parent)
	local character = MyPlayer.Character

	local animation = character:WaitForChild("Humanoid").Animator:LoadAnimation(script:WaitForChild("Animation"))
	animation:Play()
    if Tool.Unequipped then
    animation:Stop()
end

local uis = game:GetService("UserInputService");

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
		print("sus")--Also, could be written as [[inputObject.KeyCode == "R"]]
		if ammo.Value < 10 and a then
			a = false
			local x = 10 - ammo.Value
			wait(1)
			maxammo.Value = maxammo.Value - x
			ammo.Value = 10
		end
			    end
		end)


Tool.Equipped:Connect(OnEquipped)
Tool.Activated:Connect(OnActivated)

can you try this and see what it says?

Try adding wait() in the start of the script.

i realized that i have already defined what play is, but when I use player.character, it says that the character don’t exist…

local GRAVITY_ACCELERATION = workspace.Gravity
local ammo = script.Parent.Ammo
local maxammo = script.Parent.MaxAmmo
local RELOAD_TIME = 0
local ROCKET_SPEED = 1
a = true

local MISSILE_MESH_SCALE = Vector3.new(0.3,0.3,0.3)
local ROCKET_PART_SIZE = Vector3.new(0.1,0.1,1)
local DebrisService = game:GetService('Debris')
local PlayersService = game:GetService("Players")

local MyPlayer

local humanoid = MyPlayer:WaitForChild("Humanoid")

local takePinOffAnim = script.Animation
local lol = humanoid:LoadAnimation(takePinOffAnim)
local Tool = script.Parent
local ToolHandle = Tool:WaitForChild('Handle')

local MouseLoc = Tool:WaitForChild("MouseLoc",10)

local RocketScript = script:WaitForChild("Rocket")
local SwooshSound = script:WaitForChild("Swoosh")
local BoomSound = script:WaitForChild("Boom")


local Rocket = Instance.new("Part") do

	Rocket.Name = "Rocket"
	Rocket.FormFactor = Enum.FormFactor.Custom --NOTE: This must be done before changing Size
	Rocket.Size = ROCKET_PART_SIZE
	Rocket.CanCollide = false
	Rocket.Color = Color3.new(0.998901, 0.928023, 0.0595712)

	-- Add the mesh
	local mesh = Instance.new('SpecialMesh', Rocket)

	mesh.Scale = MISSILE_MESH_SCALE
	local a1 =  Instance.new('Attachment', Rocket)
	a1.Position = Vector3.new(0, 0.043, 0.001)
	local a2 =  Instance.new('Attachment', Rocket)
	a2.Position = Vector3.new(0, -0.035, -0.013)
	-- Add fire
	local fire = Instance.new('Trail', Rocket)
	fire.Attachment0 = a1
	fire.Attachment1 = a2
	fire.Lifetime = 1
	fire.MaxLength = 1

	-- Add a force to counteract gravity
	local bodyForce = Instance.new('BodyForce', Rocket)
	bodyForce.Name = 'Antigravity'
	bodyForce.Force = Vector3.new(0, Rocket:GetMass() * GRAVITY_ACCELERATION, 0)

	-- Clone the sounds and set Boom to PlayOnRemove
	local swooshSoundClone = SwooshSound:Clone()
	swooshSoundClone.Parent = Rocket
	local boomSoundClone = BoomSound:Clone()
	boomSoundClone.PlayOnRemove = true
	boomSoundClone.Parent = Rocket

	-- Attach creator tags to the rocket early on
	local creatorTag = Instance.new('ObjectValue', Rocket)
	creatorTag.Value = MyPlayer
	creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
	local iconTag = Instance.new('StringValue', creatorTag)
	iconTag.Value = Tool.TextureId
	iconTag.Name = 'icon'

	-- Finally, clone the rocket script and enable it
	local rocketScriptClone = RocketScript:Clone()
	rocketScriptClone.Parent = Rocket
	wait(1)
	rocketScriptClone.Disabled = false
end


local function OnActivated()
	if ammo.Value > 0 then
	local myModel = MyPlayer.Character
	if Tool.Enabled and myModel and myModel:FindFirstChildOfClass("Humanoid") and myModel.Humanoid.Health > 0 then
		Tool.Enabled = false
		local Pos = MouseLoc:InvokeClient(MyPlayer)

		local rocketClone = Rocket:Clone()
		DebrisService:AddItem(rocketClone, 30)
		rocketClone.BrickColor = BrickColor.Black()
		ammo.Value = ammo.Value - 1
		local spawnPosition = (ToolHandle.CFrame * CFrame.new(0, 0, 0.5)).p
		rocketClone.CFrame = CFrame.new(spawnPosition) --I CHANGED IT TO ONLY SPAWN POSITION, THIS WAS SPAWNPOSITION + POS,
		rocketClone.Velocity = Pos * ROCKET_SPEED --NOTE: This should be done before assigning Parent
		rocketClone.Parent = workspace
			rocketClone:SetNetworkOwner(nil)
		

		wait(0.25)

		Tool.Enabled = true
		end
	else
		if ammo.Value == 0 then
			if a then
				a = false
				wait(1)
				a = true
			ammo.Value = 10
				maxammo.Value = maxammo.Value - 10
				end
		end
	end
	
	
end
function OnEquipped()
	MyPlayer = PlayersService:GetPlayerFromCharacter(Tool.Parent)


	
	

	lol:Play()

end
function OnUnequipped()
	--MyPlayer = PlayersService:GetPlayerFromCharacter(Tool.Parent)
	local Players = game:GetService("Players")

	--local player = Players.LocalPlayer
	

	lol:Stop()

end
local uis = game:GetService("UserInputService");

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
		print("sus")--Also, could be written as [[inputObject.KeyCode == "R"]]
		if ammo.Value < 10 and a then
			a = false
			local x = 10 - ammo.Value
			wait(1)
			maxammo.Value = maxammo.Value - x
			ammo.Value = 10
		end
			    end
		end)

Tool.Unequipped:Connect(OnUnequipped)
Tool.Equipped:Connect(OnEquipped)
Tool.Activated:Connect(OnActivated)

also for your script theers a error at the tag because there is no end to close function

hmm don’t work

wait()
local GRAVITY_ACCELERATION = workspace.Gravity
local ammo = script.Parent.Ammo
local maxammo = script.Parent.MaxAmmo
local RELOAD_TIME = 0
local ROCKET_SPEED = 1
a = true

local MISSILE_MESH_SCALE = Vector3.new(0.3,0.3,0.3)
local ROCKET_PART_SIZE = Vector3.new(0.1,0.1,1)
local DebrisService = game:GetService('Debris')
local PlayersService = game:GetService("Players")

local MyPlayer

local humanoid = MyPlayer.Character:WaitForChild("Humanoid")

local takePinOffAnim = script.Animation
local lol = humanoid:LoadAnimation(takePinOffAnim)
local Tool = script.Parent
local ToolHandle = Tool:WaitForChild('Handle')

local MouseLoc = Tool:WaitForChild("MouseLoc",10)

local RocketScript = script:WaitForChild("Rocket")
local SwooshSound = script:WaitForChild("Swoosh")
local BoomSound = script:WaitForChild("Boom")


local Rocket = Instance.new("Part") do

	Rocket.Name = "Rocket"
	Rocket.FormFactor = Enum.FormFactor.Custom --NOTE: This must be done before changing Size
	Rocket.Size = ROCKET_PART_SIZE
	Rocket.CanCollide = false
	Rocket.Color = Color3.new(0.998901, 0.928023, 0.0595712)

	-- Add the mesh
	local mesh = Instance.new('SpecialMesh', Rocket)

	mesh.Scale = MISSILE_MESH_SCALE
	local a1 =  Instance.new('Attachment', Rocket)
	a1.Position = Vector3.new(0, 0.043, 0.001)
	local a2 =  Instance.new('Attachment', Rocket)
	a2.Position = Vector3.new(0, -0.035, -0.013)
	-- Add fire
	local fire = Instance.new('Trail', Rocket)
	fire.Attachment0 = a1
	fire.Attachment1 = a2
	fire.Lifetime = 1
	fire.MaxLength = 1

	-- Add a force to counteract gravity
	local bodyForce = Instance.new('BodyForce', Rocket)
	bodyForce.Name = 'Antigravity'
	bodyForce.Force = Vector3.new(0, Rocket:GetMass() * GRAVITY_ACCELERATION, 0)

	-- Clone the sounds and set Boom to PlayOnRemove
	local swooshSoundClone = SwooshSound:Clone()
	swooshSoundClone.Parent = Rocket
	local boomSoundClone = BoomSound:Clone()
	boomSoundClone.PlayOnRemove = true
	boomSoundClone.Parent = Rocket

	-- Attach creator tags to the rocket early on
	local creatorTag = Instance.new('ObjectValue', Rocket)
	creatorTag.Value = MyPlayer
	creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
	local iconTag = Instance.new('StringValue', creatorTag)
	iconTag.Value = Tool.TextureId
	iconTag.Name = 'icon'

	-- Finally, clone the rocket script and enable it
	local rocketScriptClone = RocketScript:Clone()
	rocketScriptClone.Parent = Rocket
	wait(1)
	rocketScriptClone.Disabled = false
end


local function OnActivated()
	if ammo.Value > 0 then
	local myModel = MyPlayer.Character
	if Tool.Enabled and myModel and myModel:FindFirstChildOfClass("Humanoid") and myModel.Humanoid.Health > 0 then
		Tool.Enabled = false
		local Pos = MouseLoc:InvokeClient(MyPlayer)

		local rocketClone = Rocket:Clone()
		DebrisService:AddItem(rocketClone, 30)
		rocketClone.BrickColor = BrickColor.Black()
		ammo.Value = ammo.Value - 1
		local spawnPosition = (ToolHandle.CFrame * CFrame.new(0, 0, 0.5)).p
		rocketClone.CFrame = CFrame.new(spawnPosition) --I CHANGED IT TO ONLY SPAWN POSITION, THIS WAS SPAWNPOSITION + POS,
		rocketClone.Velocity = Pos * ROCKET_SPEED --NOTE: This should be done before assigning Parent
		rocketClone.Parent = workspace
			rocketClone:SetNetworkOwner(nil)
		

		wait(0.25)

		Tool.Enabled = true
		end
	else
		if ammo.Value == 0 then
			if a then
				a = false
				wait(1)
				a = true
			ammo.Value = 10
				maxammo.Value = maxammo.Value - 10
				end
		end
	end
	
	
end
function OnEquipped()
	MyPlayer = PlayersService:GetPlayerFromCharacter(Tool.Parent)


	
	

	lol:Play()

end
function OnUnequipped()
	--MyPlayer = PlayersService:GetPlayerFromCharacter(Tool.Parent)
	local Players = game:GetService("Players")

	--local player = Players.LocalPlayer
	

	lol:Stop()

end
local uis = game:GetService("UserInputService");

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
		print("sus")--Also, could be written as [[inputObject.KeyCode == "R"]]
		if ammo.Value < 10 and a then
			a = false
			local x = 10 - ammo.Value
			wait(1)
			maxammo.Value = maxammo.Value - x
			ammo.Value = 10
		end
			    end
		end)

Tool.Unequipped:Connect(OnUnequipped)
Tool.Equipped:Connect(OnEquipped)
Tool.Activated:Connect(OnActivated)

do this instead lol

animation = character:WaitForChild("Humanoid").Animator:LoadAnimation(script:WaitForChild("YOUR_ANIMATION_NAME"))