Stop playing animation when tool destroyed

Hi,

I made a thing where if the player is holding an empty basket and interacts with the proximity prompt of said model it will destroy the empty basket in their hand and replace it with a full one. This works seamlessly so far, except when I equip the full basket, it’s still playing the animation of the empty one. What’s causing this?


local emptyToolName = "LaundryBasketEmpty"
local fullToolName = "LaundryBasketFull"


local proximityPrompt = script.Parent


local function onPromptTriggered(player)

	local character = player.Character
	local backpack = player:FindFirstChild("Backpack")
	local emptyTool = character:FindFirstChild(emptyToolName) or backpack:FindFirstChild(emptyToolName)

	if emptyTool then

		emptyTool:Destroy()

		local fullTool = game.ServerStorage:FindFirstChild(fullToolName):Clone()
		fullTool.Parent = character 
	end
end


proximityPrompt.Triggered:Connect(onPromptTriggered)

Holding Empty Basket tool

image

Interact with Proximity Prompt + Receive Full Basket Tool

image

Unequipp Full Basket Tool

image

2 Likes

Can i see the code where the animation gets activated?

3 Likes

Empty Basket

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local holdingbasketAnimation = script:WaitForChild("holdingbasket")
local holdingbasket = animator:LoadAnimation(holdingbasketAnimation)

holdingbasket.Priority = Enum.AnimationPriority.Action
holdingbasket.Looped = true

local tool = script.Parent
tool.Equipped:Connect(function()
	holdingbasket:Play()
end)

tool.Unequipped:Connect(function()
	holdingbasket:Stop()
end)

Basket Full (given after interacting with prompt)

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local holdingbasketAnimation = script:WaitForChild("holdingbasket")
local holdingbasket = animator:LoadAnimation(holdingbasketAnimation)

holdingbasket.Priority = Enum.AnimationPriority.Action
holdingbasket.Looped = true

local tool = script.Parent

tool.Equipped:Connect(function()
	holdingbasket:Play()
end)

tool.Unequipped:Connect(function()
	holdingbasket:Stop()
end)

2 Likes

I think it still plays the animation from the empty basket script. it doesn’t activate the Unequipped function because it gets deleted so it runs forever

1 Like

so you could probably do something like this

tool.Destroying:Connect(function()
	holdingbasket:Stop()
end)
4 Likes
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local holdingbasketAnimation = script:WaitForChild("holdingbasket")
local holdingbasket = animator:LoadAnimation(holdingbasketAnimation)

holdingbasket.Priority = Enum.AnimationPriority.Action
holdingbasket.Looped = true

local tool = script.Parent

tool.Equipped:Connect(function()
	holdingbasket:Play()
end)

tool.Destroying:Connect(function()
	holdingbasket:Stop()
end)

tool.Unequipped:Connect(function()
	holdingbasket:Stop()
end)

I added it to the script, but it sadly didn’t fix the issue:

1 Like

I also discovered that if I have the empty basket in my backpack and use the prompt without holding the basket, it gives me the full basket as expected. Then, when I unequip the full basket, the holding animation stops.

But when I’m holding the empty and use the prompt the animation continues playing even when I unequip

Do this and tell me what it prints out

tool.Destroying:Connect(function()
	holdingbasket:Stop()
    print(holdingbasket.IsPlaying)
end)

Have you tried forcefully unequipping the tool before its destroyed?

humanoid:UnequipTools()

2 Likes

I tried this, but nothing:

	if emptyTool then
		
		character:WaitForChild("Humanoid"):UnequipTools()
		emptyTool:Destroy()
		

		local fullTool = game.ServerStorage:FindFirstChild(fullToolName):Clone()
		fullTool.Parent = character 
	end
end

It doesn’t print anything for me :confused:

try this

character:WaitForChild("Humanoid"):UnequipTools()
task.wait()
emptyTool:Destroy()

I think it isn’t working because both scripts Loads the animation separately. Try this instead of .Destroying because i dont think roblox has fullly implemented that one yet.

tool:GetPropertyChangedSignal("Parent"):Connect(function()
	if not tool.Parent then
       holdingbasket:Stop()
    end
end)
1 Like

Nevermind that one, i think i found the solution.
You put this snippet of code inside the script that destroys the tool

for _, track in pairs(player.Character.Humanoid.Animator:GetPlayingAnimationTracks()) do
	thisTrack:Stop()
end
1 Like

thistrack seems to be underlined for me

It sadly didn’t work for me :confused:

i think they meant to do

for _, track in player.Character.Humanoid.Animator:GetPlayingAnimationTracks() do
	track:Stop()
end

Oh. That still didn’t do anything for me :confused:

print track in the for loop then look at the output

for _, track in pairs(character.Humanoid.Animator:GetPlayingAnimationTracks()) do
   print(track)
   track:Stop()
end

This is what I get in output
image