Pet position issue

Hello I’m creating a Pet System and I have an issue with the pet script, every time I jump above a mesh for example or a base block of Roblox, my character starts to be tipped 10 studs away, but this problem happens only when I jump, someone has a solution ?

PS: the games gives to me this error: “Workspace.Ironboy1965.Dog1.PetMotionScript:22: attempt to index nil with ‘Position’”

Here’s the script:

RunService.Heartbeat:Connect(function()

	local ray = Ray.new(script.Parent.HumanoidRootPart.Position, -script.Parent.HumanoidRootPart.CFrame.UpVector * 1000)
	local part = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent.Parent})

	local groundY = part.Position.Y + (part.Size.Y / 2)

	local goalCF = HumanoidRootPart.CFrame + Vector3.new(0, groundY + 1.45 - HumanoidRootPart.Position.Y, 2) - (HumanoidRootPart.CFrame.RightVector + HumanoidRootPart.CFrame.LookVector) * 2.3

	script.Parent.HumanoidRootPart.BodyPosition.Position = goalCF.Position
	script.Parent.HumanoidRootPart.BodyGyro.CFrame = goalCF

	if script.Parent.HumanoidRootPart.AssemblyLinearVelocity.Magnitude > 6 then
		
		  if not WalkAnimation.IsPlaying then  WalkAnimation:Play() IdleAnimation:Stop() end
		 
	else
		
		IdleAnimation:Play()
		WalkAnimation:Stop()
		
	end

end)