How would I make my bot find the nearest node?

I know I need to use magnitude but anytime I try to actually script it I can’t seem to make it work. I don’t know if I’m dumb or it just doesn’t work at all.


– the red spots are the nodes its a test map

1 Like

By doing :GetChildren() you get a table with all the Children of something, so what you have to do is loop through all of them and find the closest one to the Bot.
This can be done by something like this for example:

function patrol()
   local waypoints = workspace.Nodes:GetChildren()
   local closest = nil
   local distance = 3
   for _,node in pairs(waypoints) do
       if (node.Position-monster.HumanoidRootPart.Position).Magnitude < distance then
         closest = node
         distance = (node.Position-monster.HumanoidRootPart.Position).Magnitude
       end
   end
   if closest ~= nil then
      monster:MoveTo(waypoints.Position)
   end
end
2 Likes

Thank you I’ll try this! (3O characters)

1 Like

I have a game where I use a custom navmesh and finding the nearest navmesh among almost 10,000 for 100 NPCs gets a bit hectic.

Instead, to scale your game for larger situations, you can use WorldRoot:FindPartsInRegion3WithWhiteList (roblox.com) to find the node nearest.

1 Like