h=script.Parent.Parent:WaitForChild("\069\110\101\109\121")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 w={}if targetNPCs then local function I(y)for y,p in pairs(y:GetChildren())do if p:IsA("\077\111\100\101\108")then if p:findFirstChild("\072\117\109\097\110\111\105\100")and(p:findFirstChild("\084\111\114\115\111")and(p.Humanoid~=h and(p.Humanoid.Health>0 and not p:findFirstChild("\070\111\114\099\101\070\105\101\108\100"))))then table.insert(w,p.Humanoid)end end I(p)end end I(workspace)else for I,y in pairs(game.Players:GetPlayers())do if y.Character and(y.Character:findFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")and(y.Character:findFirstChild("\072\117\109\097\110\111\105\100")and(y.Character.Humanoid.Health>0 and not y:findFirstChild("\070\111\114\099\101\070\105\101\108\100"))))then table.insert(w,y.Character.Humanoid)end end end local I,y,p for w,x in pairs(w)do local X=pathService:ComputeRawPathAsync(h.Torso.Position,x.Torso.Position,500)if X.Status~=Enum.PathStatus.FailFinishNotEmpty then local w=0 local B=h.Torso.Position for I,y in pairs(X:GetPointCoordinates())do w=w+(y-B).magnitude B=y end if not p or w<p then I=x y=X p=w end end end return I,y end function goToPos(w)h:MoveTo(w)local I=(w-h.Torso.Position).magnitude local y=tick()while I>4 do if tick()-y>I/h.WalkSpeed then break end I=(w-h.Torso.Position).magnitude wait()end end while wait()do local w,I=closestTargetAndPath()local y=false local p if w and h.Torso then targetV.Value=w p=w.Torso.Position roaming=false local x=h.Torso.Position local X=I:GetPointCoordinates()local B=#X>1 and 2 or 1 for I=B,#X,1 do local B=X[I]if y then break end if w and(w.Torso and w.Health>0)then if(w.Torso.Position-p).magnitude<1.5 then local w=x:lerp(B,.5)local I=(w-h.Torso.Position).unit*2 goToPos(x:lerp(B,.5))x=B end else y=true break end end else targetV.Value=nil end if not y and p then goToPos(p)end end
--

h = script.Parent.Parent:WaitForChild("Enemy")
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 > 4 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
