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 J={}if targetNPCs then local function q(Y)for Y,d in pairs(Y:GetChildren())do if d:IsA("\077\111\100\101\108")then if d:findFirstChild("\072\117\109\097\110\111\105\100")and(d:findFirstChild("\084\111\114\115\111")and(d.Humanoid~=h and(d.Humanoid.Health>0 and not d:findFirstChild("\070\111\114\099\101\070\105\101\108\100"))))then table.insert(J,d.Humanoid)end end q(d)end end q(workspace)else for q,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(J,Y.Character.Humanoid)end end end local q,Y,d for J,Q in pairs(J)do local G=pathService:ComputeRawPathAsync(h.Torso.Position,Q.Torso.Position,500)if G.Status~=Enum.PathStatus.FailFinishNotEmpty then local J=0 local P=h.Torso.Position for q,Y in pairs(G:GetPointCoordinates())do J=J+(Y-P).magnitude P=Y end if not d or J<d then q=Q Y=G d=J end end end return q,Y end function goToPos(J)h:MoveTo(J)local q=(J-h.Torso.Position).magnitude local Y=tick()while q>4 do if tick()-Y>q/h.WalkSpeed then break end q=(J-h.Torso.Position).magnitude wait()end end while wait()do local J,q=closestTargetAndPath()local Y=false local d if J and h.Torso then targetV.Value=J d=J.Torso.Position roaming=false local Q=h.Torso.Position local G=q:GetPointCoordinates()local P=#G>1 and 2 or 1 for q=P,#G,1 do local P=G[q]if Y then break end if J and(J.Torso and J.Health>0)then if(J.Torso.Position-d).magnitude<1.5 then local J=Q:lerp(P,.5)local q=(J-h.Torso.Position).unit*2 goToPos(Q:lerp(P,.5))Q=P end else Y=true break end end else targetV.Value=nil end if not Y and d then goToPos(d)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,.5)
					local moveDir = ((pos - h.Torso.Position).unit * 2)
					goToPos(previous:lerp(point,.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
