So yeah this is a Pathfinding code that is inside my NPC, works fine and everything however.. I took an interest to Pathfinding recently..

I want the NPC to go to each nodes in order? say "NODE001" first then "NODE002" and so on.. I was wondering if any one can explain how to do this?

Please help!

local p=game:GetService("PathfindingService")
local nodes={}
for _,v in pairs(workspace:GetChildren())do
	if v.Name=="NODE001"then
		nodes[#nodes+1]=v
	end
end

for _,v in pairs(workspace.CAKEMAN:GetChildren())do
	wait(.1)
	Spawn(function()
		while wait()do
			local target=nodes[math.random(1,#nodes)]
			local path=p:ComputeRawPathAsync(v.Torso.Position,target.Position,500)
			if path.Status==Enum.PathStatus.Success then
				local points=path:GetPointCoordinates()
				for _,point in pairs(points)do
					local part,P=workspace:FindPartOnRay(Ray.new(point,Vector3.new(0,-10,0)))
					if part then
						point=P
						points[_]=P
					end
				end
				local br=false
				local distance;
				for _=1,#points,2 do
					local point=points[_]
					if br then
						break
					end
					local count=0
					repeat
						count=count+1
						if count>100 then
							br=true
							break
						end
						v.Student:MoveTo(points[math.min(#points,_+1)])
						if point.Y>=v.Student.Torso.Position.Y-1 then
							v.Student.Jump=true
						end
						distance=(point*Vector3.new(1,0,1)-v.Torso.Position*Vector3.new(1,0,1)).magnitude
						wait()
					until distance<5
				end
			else
				v.Student:MoveTo(v.Torso.CFrame*Vector3.new(0,0,5))
			end
		end
	end)
end

2 comments

0

So is your question trying to figure out how to sort the nodes into a table?

0

Yeah pretty much, I don't have a clue on how to get the NPC to go to "NODE001" to " NODE002" etc