local k=script.Parent local a=k:WaitForChild("\072\117\109\097\110\111\105\100")local P=k:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")local z=PFS:CreatePath({WaypointSpacing=math.huge})local S=PFS:CreatePath({WaypointSpacing=math.huge})local B local M local function R()while wait()do local a,S=nil,nil local B local R for M,Q in pairs(workspace:GetChildren())do if Q:FindFirstChild("\072\117\109\097\110\111\105\100")and(Q~=k and(not(Q:FindFirstChild("\078\101\120\116\098\111\116")or Q:FindFirstChild("\078\080\067"))and(Q.Humanoid.Health>0 and Q:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116"))))then z:ComputeAsync(P.Position,Q.HumanoidRootPart.Position)if z.Status==Enum.PathStatus.Success then B=0 for k,a in ipairs(z:GetWaypoints())do if k~=1 then B=B+(a.Position-R).Magnitude end R=a.Position end if S==nil then S=B a=Q else if B<S then S=B a=Q end end end end end M=a end end spawn(R)local Q=0 RunS.Heartbeat:Connect(function()if Q>=3 then Q=0 if M~=nil and M:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")then S:ComputeAsync(P.Position,M.HumanoidRootPart.Position)for k,P in ipairs(S:GetWaypoints())do if k~=1 then a:MoveTo(P.Position)a.MoveToFinished:Wait()end end end else Q=Q+1 end end)


local bot = script.Parent -- bot stuff
local bhum = bot:WaitForChild('Humanoid')
local bhrp = bot:WaitForChild('HumanoidRootPart')

local findPath = PFS:CreatePath({WaypointSpacing = math.huge})
local actPath = PFS:CreatePath({WaypointSpacing = math.huge})

local followedChar -- the one we are already following

local char -- the one which we follow

local function nearestChar()
	while wait() do
		local nearChar, nearDist = nil, nil
		local distance
		local firstPos

		for i,v in pairs(workspace:GetChildren()) do
			if v:FindFirstChild('Humanoid') and v ~= bot and not (v:FindFirstChild('Nextbot') or v:FindFirstChild('NPC')) and v.Humanoid.Health > 0 and v:FindFirstChild('HumanoidRootPart') then
				findPath:ComputeAsync(bhrp.Position, v.HumanoidRootPart.Position)
				if findPath.Status == Enum.PathStatus.Success then
					distance = 0
					for i,waypoint in ipairs(findPath:GetWaypoints()) do
						if i ~= 1 then
							distance = distance + (waypoint.Position - firstPos).Magnitude
						end
						firstPos = waypoint.Position
					end
					if nearDist == nil then
						nearDist = distance
						nearChar = v
					else
						if distance < nearDist then
							nearDist = distance
							nearChar = v
						end
					end
				end
			end
		end
		char = nearChar
	end
end

spawn(nearestChar)

local count = 0

RunS.Heartbeat:Connect(function()
	if count >= 3 then -- You may change this number to your liking, but if you set it too low nextbot will lag
		count = 0
		if char ~= nil and char:FindFirstChild('HumanoidRootPart') then
			actPath:ComputeAsync(bhrp.Position, char.HumanoidRootPart.Position)
			for i,v in ipairs(actPath:GetWaypoints()) do
				if i ~= 1 then
					bhum:MoveTo(v.Position)
					bhum.MoveToFinished:Wait()
				end
			end
		end
	else
		count = count + 1
	end
end)
