How do I stop the animation when the tool gets unequipped?

Hello,

In my tool “Wheelbarrow” is a local script which does a animation when the tool gets equipped.
But I didn’t figure out how to make the animation stop when the tool gets unequipped. Can someone help me?
Thats the script:

local Tool = script.Parent
local R15 = script:WaitForChild(“R15”, 1) – Skript finden R15 Animation
local R6 = script:WaitForChild(“R6”, 1) – Skript finden R6 Animation

Tool.Equipped:Connect(function()
local Character = Tool.Parent
if Character then
local Humanoid = Character:FindFirstChildWhichIsA(“Humanoid”)
if Humanoid then
local LoadedAnim = nil
if Humanoid.RigType == Enum.HumanoidRigType.R15 and R15 then
LoadedAnim = Humanoid:LoadAnimation(R15)
elseif Humanoid.RigType == Enum.HumanoidRigType.R6 and R6 then
LoadedAnim = Humanoid:LoadAnimation(R6)
end
if LoadedAnim then
LoadedAnim:Play()
end
end
end
end)

1 Like
local LoadedAnimation

Tool.Equiped:Connect(function()
--Stuff
end)

Tool.Unequiped:Connect(function()
   if LoadedAnimation then
      LoadedAnimation:Stop()
      LoadedAnimation = nil
   end
end)


I got an error and it doesn’t work. Did I do anything wrong?

local Tool = script.Parent
local R15 = script:WaitForChild("R15", 1) -- R15 Animation
local R6 = script:WaitForChild("R6", 1) -- R6 Animation

local LoadedAnim 

Tool.Equipped:Connect(function()
	local Character = Tool.Parent
	if not Character then return end 
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	if not Humanoid then return end 
	if Humanoid.RigType == Enum.HumanoidRigType.R15 and R15 then
		LoadedAnim = Humanoid:LoadAnimation(R15)
		LoadedAnim:Play()
	elseif Humanoid.RigType == Enum.HumanoidRigType.R6 and R6 then
		LoadedAnim = Humanoid:LoadAnimation(R6)
		LoadedAnim:Play()
	end
end)

function StopAnim()
	if LoadedAnim then 
		LoadedAnim:Stop()
	end
end

Tool.Unequipped:Connect(StopAnim)

--creating a different thread, so even if the script gets destroyed it will still run
task.spawn(function()
	while Tool.Parent ~= nil do 
		task.wait(.05) 
	end
	StopAnim()
end)
1 Like

You forgot to complete these if statements with end.

if Character then
    local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
    if Humanoid then
        local LoadedAnim = nil
        if Humanoid.RigType == Enum.HumanoidRigType.R15 and R15 then
            LoadedAnim = Humanoid:LoadAnimation(R15)
        elseif Humanoid.RigType == Enum.HumanoidRigType.R6 and R6 then
            LoadedAnim = Humanoid:LoadAnimation(R6)
         end
     end
end

Ok that works, tysm :slight_smile: Can you add that if the tool gets removed (not unequipped) the animation also gets back to normal?

1 Like

your missing like 4 ends buddy

I believe it gets the unequipped is called even if the tool is removed, just test it and see, but I think this is true.

No, I removed the tool and the animation was stuck. :confused:

LocalScript under the tool:

local Player = game.Players.LocalPlayer
local AnimationTrack = script.Animation -- Add animation inside the script.
local Animator = Player.Character:WaitForChild("Humanoid"):FindFirstChildOfClass("Animator")

local Animation = Animator:LoadAnimation(AnimationTrack)

script.Parent.Equipped:Connect(function()
    Animation:Play()
end)

script.Parent.Unequipped:Connect(function()
   if Animation then
        Animation:Stop()
        task.wait(0.01)
    end
end)

Let me know if it works!

I fixed my code snippet, hence why I liked your reply.

1 Like

Doesn’t work :confused: Unequipping works fine but if I remove it when its equipped it breaks.
image

The problem is, I need it for both, R15 and R6

I fixed it using a bit of a dirty trick due to Roblox not firing the .AncestryChanged event.

It works thank you so much!! :slight_smile:

1 Like

Can you help me with another minor problem?
image

I get this error when touching the part but its still working.
Script:

script.Parent.Touched:Connect(function(hit)
local Sound = game.SoundService.FirstTask
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local lemon = player.Backpack:FindFirstChild(“Wheelbarrow”) or player.Character:FindFirstChild(“Wheelbarrow”) --Replace with your tool name
if lemon then
local char = player.Character
local equippedTool = char and char:FindFirstChildWhichIsA(“Tool”)
if equippedTool then
local ArrowEvent = game.ReplicatedStorage.ArrowEvent
ArrowEvent:FireAllClients(script.Parent, false)
lemon:Destroy()
end
end
end)

That means that Backpack doesn’t exist. Add this line after the player variable:

if not player then return end
1 Like

Which player variable, sorry :joy:

You mean this:

local lemon = player.Backpack:FindFirstChild(“Wheelbarrow”) or player.Character:FindFirstChild(“Wheelbarrow”) if not player then return end

Or a different variable?

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end

The error is gone, but the script still doesn’t work, probably because of the sound. Should I turn it into a LocalScript? Would I need to change anything then?