local l=script.Parent local G=l:WaitForChild("\072\117\109\097\110\111\105\100")local M=l:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")local C=PFS:CreatePath({WaypointSpacing=math.huge})local s=PFS:CreatePath({WaypointSpacing=math.huge})local k local w local function W()while wait()do local G,s=nil,nil local k local W for w,H in pairs(workspace:GetChildren())do if H:FindFirstChild("\072\117\109\097\110\111\105\100")and(H~=l and(H.Humanoid.Health>0 and H:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")))then C:ComputeAsync(M.Position,H.HumanoidRootPart.Position)if C.Status==Enum.PathStatus.Success then k=0 for l,G in ipairs(C:GetWaypoints())do if l~=1 then k=k+(G.Position-W).Magnitude end W=G.Position end if s==nil then s=k G=H else if k<s then s=k G=H end end end end end w=G end end spawn(W)local H=0 RunS.Heartbeat:Connect(function()if H>=3 then H=0 if w~=nil and w:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")then s:ComputeAsync(M.Position,w.HumanoidRootPart.Position)for l,M in ipairs(s:GetWaypoints())do if l~=1 then G:MoveTo(M.Position)G.MoveToFinished:Wait()end end end else H=H+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 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)
