Pet animation not working

Hello Everybody,

I working on a script that controls pet movements. The script basically detects if you click a coin or chest if so it checks all the pets you have and makes the pet attack the target. Kind of like the pet sim X attacking system or the one in other simulator games.
Problem
I am trying to make an animation where the pet moves forward facing the chest then it moves backward after 1 second. The problem is that the function known as “petAnimation” controlling the animation will not fire. No errors

Server Script

local CD = script.Parent -- click detector 
local Hitbox = script.Parent.Parent
local Health = Hitbox.Parent.Health

game.Players.PlayerAdded:Connect(function(plr)


	local function DetectClick(playerWhoClicked)
		-- CD function
		local amountequuippedvalue = plr.PlayerGui.Inventory.LowerInvFrame.AmountEquipped.AmountEquippedValue-- value used to track the specified amount of pets equipped
		local character = playerWhoClicked.Character:GetChildren() or playerWhoClicked.CharacterAdded:Wait():GetChildren()-- getting char

		for _,pet in pairs(character) do


			if pet ~= nil then 
				if pet:IsA("Model")  then
					-- if can a print, it will print the pet name ex:Dragon 
					print("Pet Found")

					pet:PivotTo(Hitbox.CFrame)

					local alignPosition = pet:WaitForChild("AlignPosition")
					local alignOrientation = pet:WaitForChild("AlignOrientation")
					local attachmentpet = pet.PetHitBox:WaitForChild("Attachment")
					local atttachmentTarget = Hitbox.Attachment
					-- all these attachments and constrainst were created before hand
					attachmentpet.Parent = pet.PrimaryPart

					alignPosition.Attachment0 = attachmentpet
					alignPosition.Attachment1 = atttachmentTarget
					alignPosition.Parent = Hitbox
					-- for positioning the pet 
					alignOrientation.Attachment0 = attachmentpet
					alignOrientation.Attachment1 = atttachmentTarget
					alignOrientation.Parent = Hitbox
					-- for rotating the pet



					if amountequuippedvalue.Value == 1 then 
						atttachmentTarget.Position = Vector3.new(0,0,4) 
					elseif amountequuippedvalue.Value == 2 then 
						atttachmentTarget.Position = Vector3.new(6,0,0) 
						atttachmentTarget.Orientation = Vector3.new(0,87,0)
					elseif amountequuippedvalue.Value == 3 then 
						atttachmentTarget.Orientation = Vector3.new(0,185,0)
						atttachmentTarget.Position = Vector3.new(0,0,-4)
					elseif amountequuippedvalue.Value == 4 then 
						atttachmentTarget.Position = Vector3.new(-5,0,0)
						atttachmentTarget.Orientation = Vector3.new(0,-90,0)
					elseif amountequuippedvalue.Value == 5 then 
						atttachmentTarget.Position = Vector3.new(0,0,3)

						local function petAnimation()

							local atttachmentTarget = Hitbox.Attachment
							while Health.Value == 100 do 
								wait(1)
								pet.CFrame = pet.CFrame + pet.CFrame.LookVector * 2
								
								wait(1)
								pet.CFrame = pet.CFrame + pet.CFrame.LookVector:Inverse() * 2

								petAnimation()
							end

						end





					end

				end


			else 
				print("There was a issue with the pet loading")

			end
		en
	end
	CD.MouseClick:Connect(DetectClick)

end)

You can’t recall a function from inside a function.