h=script.Parent.Parent:WaitForChild("\072\117\109\097\110\111\105\100")pathService=game:GetService("\080\097\116\104\102\105\110\100\105\110\103\083\101\114\118\105\099\101")targetV=script.Parent:WaitForChild("\084\097\114\103\101\116")function closestTargetAndPath()local m={}if targetNPCs then local function o(c)for c,n in pairs(c:GetChildren())do if n:IsA("\077\111\100\101\108")then if n:findFirstChild("\072\117\109\097\110\111\105\100")and(n:findFirstChild("\084\111\114\115\111")and(n.Humanoid~=h and(n.Humanoid.Health>0 and not n:findFirstChild("\070\111\114\099\101\070\105\101\108\100"))))then table.insert(m,n.Humanoid)end end o(n)end end o(workspace)else for o,c in pairs(game.Players:GetPlayers())do if c.Character and(c.Character:findFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")and(c.Character:findFirstChild("\072\117\109\097\110\111\105\100")and(c.Character.Humanoid.Health>0 and not c:findFirstChild("\070\111\114\099\101\070\105\101\108\100"))))then table.insert(m,c.Character.Humanoid)end end end local o,c,n for m,p in pairs(m)do local i=pathService:ComputeRawPathAsync(h.Torso.Position,p.Torso.Position,500)if i.Status~=Enum.PathStatus.FailFinishNotEmpty then local m=0 local D=h.Torso.Position for o,c in pairs(i:GetPointCoordinates())do m=m+(c-D).magnitude D=c end if not n or m<n then o=p c=i n=m end end end return o,c end function goToPos(m)h:MoveTo(m)local o=(m-h.Torso.Position).magnitude local c=tick()while o>100 do if tick()-c>o/h.WalkSpeed then break end o=(m-h.Torso.Position).magnitude wait()end end while wait()do local m,o=closestTargetAndPath()local c=false local n if m and h.Torso then targetV.Value=m n=m.Torso.Position roaming=false local p=h.Torso.Position local i=o:GetPointCoordinates()local D=#i>1 and 2 or 1 for o=D,#i,1 do local D=i[o]if c then break end if m and(m.Torso and m.Health>0)then if(m.Torso.Position-n).magnitude<1.5 then local m=p:lerp(D,.5)local o=(m-h.Torso.Position).unit*2 goToPos(p:lerp(D,.5))p=D end else c=true break end end else targetV.Value=nil end if not c and n then goToPos(n)end end
--

h = script.Parent.Parent:WaitForChild("Humanoid")
pathService = game:GetService("PathfindingService")
targetV = script.Parent:WaitForChild("Target")

function closestTargetAndPath()
	local humanoids = {}
	if targetNPCs then
		local function recurse(o)
			for _, obj in pairs(o:GetChildren()) do
				if obj:IsA("Model") then
					if
						obj:findFirstChild("Humanoid")
						and obj:findFirstChild("Torso")
						and obj.Humanoid ~= h
						and obj.Humanoid.Health > 0
						and not obj:findFirstChild("ForceField")
					then
						table.insert(humanoids, obj.Humanoid)
					end
				end
				recurse(obj)
			end
		end
		recurse(workspace)
	else
		for _, v in pairs(game.Players:GetPlayers()) do
			if
				v.Character
				and v.Character:findFirstChild("HumanoidRootPart")
				and v.Character:findFirstChild("Humanoid")
				and v.Character.Humanoid.Health > 0
				and not v:findFirstChild("ForceField")
			then
				table.insert(humanoids, v.Character.Humanoid)
			end
		end
	end
	local closest, path, dist
	for _, humanoid in pairs(humanoids) do
		local myPath = pathService:ComputeRawPathAsync(h.Torso.Position, humanoid.Torso.Position, 500)
		if myPath.Status ~= Enum.PathStatus.FailFinishNotEmpty then
			-- Now that we have a successful path, we need to figure out how far we need to actually travel to reach this point.
			local myDist = 0
			local previous = h.Torso.Position
			for _, point in pairs(myPath:GetPointCoordinates()) do
				myDist = myDist + (point - previous).magnitude
				previous = point
			end
			if not dist or myDist < dist then -- if true, this is the closest path so far.
				closest = humanoid
				path = myPath
				dist = myDist
			end
		end
	end
	return closest, path
end

function goToPos(loc)
	h:MoveTo(loc)
	local distance = (loc - h.Torso.Position).magnitude
	local start = tick()
	while distance > 100 do
		if tick() - start > distance / h.WalkSpeed then -- Something may have gone wrong. Just break.
			break
		end
		distance = (loc - h.Torso.Position).magnitude
		wait()
	end
end

while wait() do
	local target, path = closestTargetAndPath()
	local didBreak = false
	local targetStart
	if target and h.Torso then
		targetV.Value = target
		targetStart = target.Torso.Position
		roaming = false
		local previous = h.Torso.Position
		local points = path:GetPointCoordinates()
		local s = #points > 1 and 2 or 1
		for i = s, #points do
			local point = points[i]
			if didBreak then
				break
			end
			if target and target.Torso and target.Health > 0 then
				if (target.Torso.Position - targetStart).magnitude < 1.5 then
					local pos = previous:lerp(point, 0.5)
					local moveDir = ((pos - h.Torso.Position).unit * 2)
					goToPos(previous:lerp(point, 0.5))
					previous = point
				end
			else
				didBreak = true
				break
			end
		end
	else
		targetV.Value = nil
	end
	if not didBreak and targetStart then
		goToPos(targetStart)
	end
end
