Stopping an animation when a Tool is destroyed

I’m trying to figure out a way to stop custom tool equip animations when they’re deleted. For context, my game has tools that play custom equip animations. When unequipped normally, the animations stop, but when deleted or parented to nil they don’t, which can cause issues with VIP server commands. I believe the cause is the tool script being parented to nil, stopping it from running. If so, how can I get around this?

The code looks a little like:

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Animation = Humanoid:LoadAnimation(script.Animation)
local Tool = script.Parent

Tool.Equipped:Connect(function()
	Animation:Play()
end)

Tool.Unequipped:Connect(function()
	Animation:Stop()
end)

Tool.AncestryChanged:Connect(function()
	if not Tool:IsDescendantOf(game) then Animation:Stop() end
end)
2 Likes

I had similar problem with a player deleting a vehicle while sitting in the seat.

The “Seated” animation would keep playing.

I added a LocalScript to the StarterCharacterScripts:

local function onStateChanged(_oldState, newState)
	if _oldState == Enum.HumanoidStateType.Seated then
		for i, animation in pairs(Humanoid:GetPlayingAnimationTracks()) do

			if animation.Name == "Seated" then animation:Stop() end

		end		
	end
end
Humanoid.StateChanged:Connect(onStateChanged)

For mine, the name of the animation was “Seasted” because that is what I named the animation.

You could do the same with Character.ChildRemoved instead of Humanoid.StateChanged.

local function onChildRemoved(child)
	if child:IsA("Tool") then
		for i, animation in pairs(Humanoid:GetPlayingAnimationTracks()) do

			if animation.Name == child.Name then animation:Stop() end -- Name animations same as tools

		end		

	end
end

Character.ChildRemoved:Connect(onChildRemoved)
1 Like

Also use Humanoid.Animator:LoadAnimation instead of just Humanoid:LoadAnimation to load your animations, because just using Humanoid is a deprecated method. Not much of some help for your situation, but just a useful tip down the line incase they completely shut down Humanoid:LoadAnimation.

Using AncestryChanged Method

This event includes two parameters, child and parent . Child refers to the Instance whose Instance.Parent was actually changed. Parent refers to this instance’s new Instance.Parent.

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Animation = Humanoid:LoadAnimation(script.Animation)
local Tool = script.Parent

Tool.Equipped:Connect(function()
	Animation:Play()
end)

Tool.Unequipped:Connect(function()
	Animation:Stop()
end)

Tool.AncestryChanged:Connect(function(_, parent) --/!!/  Here we pass the parent

	--/!!/  If the new parent is nil ( when destroyed ); Stop Animation
	if parent == nil then Animation:Stop() end 

end)

Using Instance.Destroying Method
(Recommended if you’re using Instance:Destroy)

The Destroying event fires immediately before the Instance or one of its ancestors is destroyed with Instance.Destroy().

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Animation = Humanoid:LoadAnimation(script.Animation)
local Tool = script.Parent

Tool.Equipped:Connect(function()
	Animation:Play()
end)

Tool.Unequipped:Connect(function()
	Animation:Stop()
end)

Tool.Destroying:Connect(function() 
	Animation:Stop() --/!!/  Stop Animation immediately before Tool is Destroyed
end)
 

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.