Stop animation when unequip tool

i have a gun animation which keep playing until the tool is unequipped, but when I unequip it, it says there is no humanoid because script.parent.parent don’t apply when I unequip the tool, here’s the script:

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

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

	local takePinOffAnim = script.Animation
	local lol = humanoid:LoadAnimation(takePinOffAnim)

	lol:Play()

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")

	local takePinOffAnim = script.Animation
	local lol = humanoid:LoadAnimation(takePinOffAnim)

	lol:Stop()

end
2 Likes

^ Solved Topic.

Searching on the devforum for your problem would’ve brought up this.

Have a nice day :grinning_face_with_smiling_eyes:,
Swift

2 Likes

no…this is in a normal script not a local script so localplayer won’t work

2 Likes

i advise you to set the variables to be a global one, so it would be like this

local character = Tool.Parent
local humanoid = character:WaitForChild("Humanoid")

local takePinOffAnim = script.Animation
local lol = humanoid:LoadAnimation(takePinOffAnim)

function OnEquipped()
	lol:Play()
end
function OnUnequipped()
	lol:Stop()
end
1 Like

hmm…that did not seem to work when i did it…the output says this line have error:

local humanoid = character:WaitForChild("Humanoid")

heres the whole local section

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 Tool = script.Parent
local ToolHandle = Tool:WaitForChild('Handle')
local character = Tool.Parent
local humanoid = character:WaitForChild("Humanoid")

local takePinOffAnim = script.Animation
local lol = humanoid:LoadAnimation(takePinOffAnim)

what error did the output said?

it just says a blue line on the 18th line(which is the humanoid part)
oddly no red lines

Blue line?, i can not understand what you are trying to say

like those blue error lines in the output?

I’m sorry but could you give me a screenshot of what you mean?

i think your supposed to make it character becaue it wouden’t work if the thing is in player’s backpack

Do animator:LoadAnimation(takePinOffAnim)

Just make a separate variable for animator.

local animator = humanoid:WaitForChild("Animator")

As Humanoid:LoadAnimation is deprecated.

where would i put that? like do i have to delete the lol thing part?

Shows that it can’t find Humanoid in backpack, which is true.

Humanoid isn’t an instance to backpack.

Instead make a character variable when you call the .Equipped event. Like this

local function OnEquipped()
    local character = Tool.Parent -- Because when tool gets equipped, its parent is the character.
end

But

local character = Tool.Parent -- It's the backpack since the tool wasn't equipped and it's stored in the backpack.

Ah, the blue line wasn’t the error, its the “Infinite yield possible”
anyways i believe this can do it correctly


local Anim

local takePinOffAnim = script.Animation

function OnEquipped()
    local character = Tool.Parent
    local humanoid = character:WaitForChild("Humanoid")
    Anim = humanoid:LoadAnimation(takePinOffAnim)
	Anim:Play()
end
function OnUnequipped()
	if Anim Then -- just here to be safe
    Anim:Stop()
    end
end

edit: forgot that tool variable exists on his code

the animation is still playing after the unequip…

After trying to debug the code on roblox studio in my low end laptop (hence the late response)
try changing the uneqquiped into this

function OnUnequipped()
    Anim:Stop()
end
local tool = script.Parent
local animation = nil

function OnEquip()
    local plr = game.Players:GetPlayerFromCharacter(tool.Parent)
    local character = plr.Character

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

function OnUnequip()
    if animation ~= nil then
        animation:Stop()
    end
end

tool.Equipped:Connect(OnEquip)
tool.Unequipped:Connect(OnUnequip)
2 Likes