local G=nil local function z(z,o)local a=z:FindFirstChild("\072\117\109\097\110\111\105\100")if not z and a then return nil end local C=a.RigType.Value==0 if table.find({"\084\111\114\115\111";"\085\112\112\101\114\084\111\114\115\111"},o)then if C then o="\084\111\114\115\111"else o="\085\112\112\101\114\084\111\114\115\111"end end local E=z:FindFirstChild(o)or z:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if E then return E else if G~=o then G=o end return nil end end local function o()local G,o=nil,LookAtPlayerRange for a=1,#SearchLoc,1 do for a,C in pairs(SearchLoc[a]:GetChildren())do if C:FindFirstChild("\072\117\109\097\110\111\105\100")and(C~=Body and((Players:GetPlayerFromCharacter(C)or LookAtNonPlayer)and z(C,PartToLookAt)))then local z=(Core.Position-C.PrimaryPart.Position).Magnitude if z<o then G=C o=z end end end end return G end local function a(G)G=G or.05 local z=os.clock()while os.clock()-z<G do(game:GetService("\082\117\110\083\101\114\118\105\099\101")).Heartbeat:Wait()end end local function C(G,z)if not IsR6 then if Neck then Neck.C0=Neck.C0:lerp(G,UpdateSpeed/2)end if Waist then Waist.C0=Waist.C0:lerp(z,UpdateSpeed/2)end else if Neck then Neck.C0=Neck.C0:lerp(G,UpdateSpeed/2)end end end


local ErrorPart = nil
local function GetValidPartToLookAt(Char, bodypart)
	local pHum = Char:FindFirstChild("Humanoid")
	if not Char and pHum then return nil end
	local pIsR6 = (pHum.RigType.Value==0)
	if table.find({"Torso", "UpperTorso"}, bodypart) then
		if pIsR6 then bodypart = "Torso" else bodypart = "UpperTorso" end
	end
	local ValidPart = Char:FindFirstChild(bodypart) or Char:FindFirstChild("HumanoidRootPart")
	if ValidPart then return ValidPart else
		if ErrorPart ~= bodypart then
			--warn(Body.Name.." can't find part to look: "..tostring(bodypart))
			ErrorPart = bodypart
		end
		return nil end
end

local function getClosestPlayer() -- Get the closest player in the range.
	local closest_player, closest_distance = nil, LookAtPlayerRange
	for i = 1, #SearchLoc do
		for _, player in pairs(SearchLoc[i]:GetChildren()) do
			if player:FindFirstChild("Humanoid") and player ~= Body 
				and (Players:GetPlayerFromCharacter(player) or LookAtNonPlayer) 
				and GetValidPartToLookAt(player, PartToLookAt) then
				
				local distance = (Core.Position - player.PrimaryPart.Position).Magnitude
				if distance < closest_distance then
					closest_player = player
					closest_distance = distance
				end
			end
		end
	end
	return closest_player
end

local function rWait(n)
	n = n or 0.05
	local startTime = os.clock()

	while os.clock() - startTime < n do
		game:GetService("RunService").Heartbeat:Wait()
	end
end

local function LookAt(NeckC0, WaistC0)
	if not IsR6 then
		if Neck then Neck.C0 = Neck.C0:lerp(NeckC0, UpdateSpeed/2) end
		if Waist then Waist.C0 = Waist.C0:lerp(WaistC0, UpdateSpeed/2) end
	else
		if Neck then Neck.C0 = Neck.C0:lerp(NeckC0, UpdateSpeed/2) end
	end
end
