--------------------------------------------


game:GetService("RunService").Heartbeat:Connect(function()
	rWait(UpdateDelay)
	local TrsoLV = Trso.CFrame.lookVector
	local HdPos = Head.CFrame.p
	local player = getClosestPlayer()
	local LookAtPart
	if Neck or Waist then
		if player then
			local success, err = pcall(function()
				LookAtPart = GetValidPartToLookAt(player, PartToLookAt)
				if LookAtPart then
					local Dist = nil;
					local Diff = nil;
					local is_in_front = Core.CFrame:ToObjectSpace(LookAtPart.CFrame).Z < 0
					if is_in_front then
						if LookingAtValue.Value ~= player then
							LookingAtValue.Value = player
						end
						
						Dist = (Head.CFrame.p-LookAtPart.CFrame.p).magnitude
						Diff = Head.CFrame.Y-LookAtPart.CFrame.Y

						if not IsR6 then
							LookAt(NeckOrgnC0*Ang(-(aTan(Diff/Dist)*HeadVertFactor), (((HdPos-LookAtPart.CFrame.p).Unit):Cross(TrsoLV)).Y*HeadHorFactor, 0), 
								WaistOrgnC0*Ang(-(aTan(Diff/Dist)*BodyVertFactor), (((HdPos-LookAtPart.CFrame.p).Unit):Cross(TrsoLV)).Y*BodyHorFactor, 0))
						else	
							LookAt(NeckOrgnC0*Ang((aTan(Diff/Dist)*HeadVertFactor), 0, (((HdPos-LookAtPart.CFrame.p).Unit):Cross(TrsoLV)).Y*HeadHorFactor))
						end
					elseif LookBackOnNil then
						LookAt(NeckOrgnC0, WaistOrgnC0)
						if LookingAtValue.Value then
							LookingAtValue.Value = nil
						end
					end
				end
			end)
		elseif LookBackOnNil then
			LookAt(NeckOrgnC0, WaistOrgnC0)
			if LookingAtValue.Value then
				LookingAtValue.Value = nil
			end
		end
	end
end)
