Animation Problem

I was doing some animations to my new game about mining
and my animation for diging is not working
Can someone help me with this ?

This is my script
2023-09-01

local anim = tool:WaitForChild("Animation")
local connection = nil
local player = nil
local char  = nil 
local track = nil 


local function onActivated()
	track:Play()
	wait(5)
	track:Stop()
end

local function onEquipped()
	player = game.Players.LocalPlayer
	connection = tool.Activated:Connect(onActivated)
	char = player.Character or player.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	track = hum.Animator:LoadAnimation(anim)
end

local function onUnequipped()
	player = nil
	char = nil
	connection:Disconnect()
	

end

tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)

ok im blind nvm

in short terms you should move this

tool.Activated:Connect(onActivated)

out of onEquipped and then try it

You are repeatedly loading the animation when it only needs to be loaded once

connection = tool.Activated:Connect(onActivated)
local function onEquipped()
	player = game.Players.LocalPlayer
	char = player.Character or player.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	track = track or hum.Animator:LoadAnimation(anim)
end