local G=script.Parent local l=G:WaitForChild("\072\117\109\097\110\111\105\100")local E=G:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")local Y=PFS:CreatePath({WaypointSpacing=math.huge})local B=PFS:CreatePath({WaypointSpacing=math.huge})local p local J local function t()while wait()do local l,B=nil,nil local p local t for J,A in pairs(workspace:GetChildren())do if A:FindFirstChild("\072\117\109\097\110\111\105\100")and(A~=G and(A.Humanoid.Health>0 and A:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")))then Y:ComputeAsync(E.Position,A.HumanoidRootPart.Position)if Y.Status==Enum.PathStatus.Success then p=0 for G,l in ipairs(Y:GetWaypoints())do if G~=1 then p=p+(l.Position-t).Magnitude end t=l.Position end if B==nil then B=p l=A else if p<B then B=p l=A end end end end end J=l end end spawn(t)local A=0 RunS.Heartbeat:Connect(function()if A>=3 then A=0 if J~=nil and J:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")then B:ComputeAsync(E.Position,J.HumanoidRootPart.Position)for G,E in ipairs(B:GetWaypoints())do if G~=1 then l:MoveTo(E.Position)l.MoveToFinished:Wait()end end end else A=A+1 end end)
--local debris = game:GetService("Debris")

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)
