When should I play the animation?

So I’m making an enemy NPC and I followed these 2 tutorials but they never made an animation tutorial so I tried to do it myself. I made the animation but have no idea when to play it, heres my code:

–Services
local RunService = game:GetService(“RunService”)
local Players = game:GetService(“Players”)

–Parts
local humanoid = script.Parent
local root = humanoid.Parent.PrimaryPart

–Values
local targetDistance = script:GetAttribute(“TargetDistance”)
local stopDistance = script:GetAttribute(“StopDistance”)
local damage = script:GetAttribute(“Damage”)
local attackDistance = script:GetAttribute(“AttackDistance”)
local attackWait = script:GetAttribute(“AttackWait”)
local lastAttack = tick()

function findNearestPlayer()
local playerList = Players:GetPlayers()

local nearestPlayer = nil
local distance = nil
local direction = nil
-- Loop through all players
for _, player in pairs(playerList) do
	
	local character = player.Character
	
	if character then
		local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)

		-- If nil then
		if not nearestPlayer then
			nearestPlayer = player
			distance = distanceVector.Magnitude
			direction = distanceVector.Unit
		elseif distanceVector.Magnitude < distance then
			nearestPlayer = player
			distance = distanceVector.Magnitude
			direction = distanceVector.Unit
		end
	end
	
	
end

return nearestPlayer, distance, direction

end

RunService.Heartbeat:Connect(function()
local nearestPlayer, distance, direction = findNearestPlayer()

if nearestPlayer then
	--If closer than target distance and farther than 5 studs
	if distance <= targetDistance and distance >= stopDistance then
		humanoid:Move(direction)
	else
		--If not in the range above then stop
		humanoid:Move(Vector3.new(0,0,0))
	end
	
	--If close enough to attack and been more than 1 second since last attack
	if distance <= attackDistance and tick() - lastAttack >= attackWait then
		lastAttack = tick()
		nearestPlayer.Character.Humanoid.Health -= damage
	end
	
end

end)