How to stop animation properly after tool is detroyed

Hello :wave:
So i made some consumabels and i wrote this entire script that firese once the toll is getting used and after that should stop the tools idle animation

local PlayAnimation = game.ReplicatedStorage.RemoteEvents:WaitForChild("PlayAnimation")
local StopAnimation = game.ReplicatedStorage.RemoteEvents:WaitForChild("StopAnimation")

local Debris = game:GetService("Debris")

local Drinkable = {
	"Alkohol",
	"RegenHealthPotion",
	"Strenght",
	"SpeedPotion",
	"WaterBottel"
}

local DrinkSound = script:WaitForChild("DrinkingSound")

local drinkAnimation = Instance.new("Animation")
drinkAnimation.AnimationId = "rbxassetid://127384671740477"

local eatAnimation = Instance.new("Animation")
eatAnimation.AnimationId = "rbxassetid://132605216306729"

local activeAnimations = {}

PlayAnimation.OnServerEvent:Connect(function(player)
	local hum = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
	if not hum then return end
	local animator = hum:FindFirstChildOfClass("Animator")
	if not animator then return end

	local char = player.Character

	for _, tool in pairs(char:GetChildren()) do
		if tool:IsA("Tool") then
			if table.find(Drinkable, tool.Name) then
				-- Drinkable item
				local animTrack = animator:LoadAnimation(drinkAnimation)
				activeAnimations[player] = animTrack

				local soundClone = DrinkSound:Clone()
				soundClone.Parent = hum

				animTrack:Play()
				task.spawn(function()
					task.wait(1)
					soundClone:Play()
					task.wait(4)
					Debris:AddItem(soundClone, 0)
				end)

			elseif tool.Name == "FoodBox" then
				-- FoodBox
				local animTrack = animator:LoadAnimation(eatAnimation)
				activeAnimations[player] = animTrack

				animTrack:Play()
			end
		end
	end
end)

StopAnimation.OnServerEvent:Connect(function(player)
	local track = activeAnimations[player]
	if track then
		if track.IsPlaying then
			track:Stop()
		end
		activeAnimations[player] = nil
	end
end)

But i tried and tried over and over again changing the script but this only happens:


Like bro now both animation play at the same time even if tool is detroyed i have this script in every tool they are similar to each other:

local Tool = script.Parent
local inputsevice = game:GetService("UserInputService")
local debris = game:GetService("Debris")
local player = game.Players.LocalPlayer
local hum = player.Character:WaitForChild("Humanoid")
local playerGui = player:WaitForChild("PlayerGui")
local healthRegen = playerGui:WaitForChild("Effects").Frame.ScrollingFrame:WaitForChild("PainKiller")

local playAnimation = game.ReplicatedStorage.RemoteEvents.PlayAnimation
local StopAnimation = game.ReplicatedStorage.RemoteEvents.StopAnimation


local using = false
local holdtask = nil
local taskIsRunning = false
local equipped = false

Tool.Equipped:Connect(function()
	equipped = true
	inputConnection = inputsevice.InputBegan:Connect(function(input, gameprozess)
		-- Prevent further processing if game is in a process like UI interaction
		if gameprozess then return end
		if not equipped then return end

		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if healthRegen.Visible == false and not taskIsRunning and Tool then
				-- Start using the tool
				using = true
				taskIsRunning = true

				playAnimation:FireServer()

				-- Set the delay task to show the effect
				holdtask = task.delay(4.5, function()
					if using and taskIsRunning then
						healthRegen.Visible = true
						debris:AddItem(Tool, 0)
					end
				end)
			elseif healthRegen.Visible == true then
				print("You already have the Effect")
			end
		end
	end)
end)

Tool.Unequipped:Connect(function()
	equipped = false
	
	if inputConnection then
		inputConnection:Disconnect()
		inputConnection = nil
	end
end)

inputsevice.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if healthRegen.Visible == false then
			-- Stop using the tool
			using = false
			if holdtask then
				StopAnimation:FireServer()
				taskIsRunning = false
				holdtask = nil
				print("Effect stopped")
			end
		end
	end
end)

I hope some one can Help me with this one
Thank You :slightly_smiling_face:

2 Likes

Replace all the “activeAnimations[player] = animTrack”
with “table.insert(activeAnimations, animTrack)”
Then, replace

	local track = activeAnimations[player]
	if track then
		if track.IsPlaying then
			track:Stop()
		end
		activeAnimations[player] = nil
	end

with

for _, track in pairs(activeAnimations) do
	if track and track.IsPlaying then
		track:Stop()
	end
end
table.clear(activeAnimations)

Idk if this will work, but I tried my best.