How can i make my npc get the nearest part?

So im scripting wandering ai for my npc i want it too walk to the closet part too him

local function Wander(WanderingAmount)
	local Nodes = Wendigo.Nodes:GetChildren()
	local NearestNode = WanderingAmount
	local CurrentNode = nil
	
	for i, Node in pairs(Nodes) do
		local TargetNode = Node
		local distance = (Wendigo.HumanoidRootPart.Position - TargetNode.position).Magnitude
		if distance < NearestNode then
			CurrentNode = Node
		end
	end
	return CurrentNode 
end
while true do
	wait()
	local TargetNode = Wander(5000)
	print(TargetNode)

end

Loop through all parts in workspace, check the distance between your NPC and the part, save the part with the lowest distance and walk to it. It should work like that. If you don’t know how to script it, I can help you.

You can save the distances between the NPC and the nodes in a table, and then use math.min to return the smallest number in that table:

local NodeDistances = {}

for i, Node in pairs(Nodes) do
		local distance = (Wendigo.HumanoidRootPart.Position - TargetNode.position).Magnitude
		table.insert(NodeDistances, distance) --Save the magnitude distance to NodeDistances
end
		local closest = math.min(table.unpack(NodeDistances) --Find the smallest number in NodeDistances
		print(closest) -- Print the distane
end

image

It prints all node locations and nil

Can you show the script? thrty chrctrs

I removed the tables part because i think its not necessary

local function Wander(WanderingAmount)
	local Nodes = Wendigo.Nodes:GetChildren()
	local NearestNode = WanderingAmount
	local CurrentNode = nil
	
	for i, Node in pairs(Nodes) do
		local NodeDistances = {}
		local TargetNode = Node
		local distance = (Wendigo.HumanoidRootPart.Position - TargetNode.position).Magnitude
		
		print(math.min(distance))

		
		
	end
	return CurrentNode 
end

It’s a loop so it’s only doing math.min for each time you measure out distance, that’s why it doesn’t work, it’s only getting one value.

math.min should be out of the loop, after it, and have more than one number to find the minimal

Question why does it print all locations first?

  14:10:33.674  224.12857055664  -  Server - WendigoAI:43
  14:10:33.674  30.507368087769  -  Server - WendigoAI:43
local function Wander()
	local Nodes = Wendigo.Nodes:GetChildren()
	for i, Node in pairs(Nodes) do
		if Node:IsA("Part") then
			local distance = (Wendigo.HumanoidRootPart.Position - Node.position).Magnitude
			local Min = math.min(distance)
			
			print(Min)
		end
	end
end
Wander()

Put print(Min) outside of the loop

its not in a loop tho

local function Wander()
	local Nodes = Wendigo.Nodes:GetChildren()
	for i, Node in pairs(Nodes) do
		if Node:IsA("Part") then
			local distance = (Wendigo.HumanoidRootPart.Position - Node.position).Magnitude
			local Min = math.min(distance)
		
		end
	end
print(Min)
end
Wander()

for i, Node in pairs(Nodes) do is a loop that repeats for each node

image

local Min

local function Wander()
	local Nodes = Wendigo.Nodes:GetChildren()
	for i, Node in pairs(Nodes) do
		if Node:IsA("Part") then
			local distance = (Wendigo.HumanoidRootPart.Position - Node.position).Magnitude
			local Min = math.min(distance)
		end
	end
print(Min)
end
Wander()

Min is now nil
image

local Distances = {}

local function Wander()
	local Nodes = Wendigo.Nodes:GetChildren()
	for i, Node in pairs(Nodes) do
		if Node:IsA("Part") then
			local distance = (Wendigo.HumanoidRootPart.Position - Node.position).Magnitude
			table.insert(Distances, distance)
		end
	end
print(math.min(Distances))
end
Wander()

14:28:01.921 Workspace.Wendigo.WendigoAI:25: invalid argument #1 to ‘min’ (number expected, got table) - Server - WendigoAI:25

Oops, my bad.

local Distances = {}

local function Wander()
	local Nodes = Wendigo.Nodes:GetChildren()
	for i, Node in pairs(Nodes) do
		if Node:IsA("Part") then
			local distance = (Wendigo.HumanoidRootPart.Position - Node.position).Magnitude
			table.insert(Distances, distance)
		end
	end
print(math.min(unpack(Distances)))
end
Wander()
2 Likes