Pathfinding waypoints taking way too much to load

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    Make an Npc choose of the 5 points, and walk to it, repeat over and over!

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    Couldnt find any…
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

function NPCManager.MoveNPC(npc)
	task.spawn(function()
		while true do
			local humanoid = npc:FindFirstChild("Humanoid")
			local root = npc:FindFirstChild("HumanoidRootPart")
			if not humanoid or not root then return end

			local waypoints = WaypointsFolder:GetChildren()
			local target = waypoints[math.random(1, #waypoints)]

			local path = PathfindingService:CreatePath()
			path:ComputeAsync(root.Position, target.Position)

			if path.Status == Enum.PathStatus.Complete then
				for _, point in ipairs(path:GetWaypoints()) do
					humanoid:MoveTo(point.Position)
					humanoid.MoveToFinished:Wait()
				end
			else
				warn("Pathfinding failed for " .. npc.Name)
			end

			task.wait(math.random(1, 3))
		end
	end)
end

this is my module script function to make the npc walk to a random point idk what could make it delay overtime

1 Like

that a very bad way to write a code.
It first of all has unneeded anonimous function that is not a closure in this stack+infenete memory leaking with while true that never gets stopped.
MoveToFinished is kinda of broken and i recomend you to use something else instead.
MoveToFinished would eventually fire but it has time out of about 5-10 sec afaik.
Also you are reinventing the wheel too much in this code

It could also be that you have some barriers that are preventing NPC from walking.

I changed the script and tried to use magnitude to check the difference between the two positions but now it’s putting the waypoints position to 0,0,0 i cant figure out whats wrong

local function MoveNPC(npc)
	task.spawn(function()
		local NPCWalking = true
		while NPCWalking do
			local humanoid = npc:FindFirstChild("Humanoid")
			if not humanoid then return end
			
			local WaypointsFolder = workspace:WaitForChild("MallWaypoints")
			local waypoints = WaypointsFolder:GetChildren()
			local target = waypoints[math.random(1, #waypoints)]

			local path = PathfindingService:CreatePath({
				
			})
			
			print("Target selected:", target.Position)
			print("NPC root:", npc:FindFirstChild("HumanoidRootPart").Position)
			local success, errorMessage = pcall(function()
				path:ComputeAsync(npc:FindFirstChild("HumanoidRootPart").Position, target.Position)
			end)
			
			if success and path.Status == Enum.PathStatus.Success then
				for _, waypoint in ipairs(path:GetWaypoints()) do
					
					--
					local part = Instance.new("Part")
					part.Name = "PathPoint"
					part.Shape = "Ball"
					part.Size = Vector3.new(2, 2, 2)
					part.Position = waypoint.Position
					part.Material = "Neon"
					part.Anchored = true
					part.Transparency = 0.67
					part.CanCollide = false
					part.Parent = workspace
					game:GetService("Debris"):AddItem(part, 10)
					--
					humanoid:MoveTo(waypoint.Position)
					print(waypoint.Position)
					
					
					local reached = false
					local time = tick()

					repeat
						task.wait(5)
						local dist = (waypoint.Position - npc:FindFirstChild("HumanoidRootPart").Position).Magnitude
						if dist <= 0.5 then
							reached = true
							break
						end
					until (tick() - time) >= 5

					if reached then
						print("reached")
					else
						print("not reached")
						break
					end
					end
			else
				warn("pf failed:" .. npc.Name)
				NPCWalking = false
			end
		end
	end)
end

Im so tired rn so im going to sleep and see what i can do tomorrow. Thanks for replying fast tho!

I can’t really tell what’s happening other than the NPC not moving

it’s most likely from the loop you have that detects if it’s distance is lower than 0.5
for some reason you made a delay of 5 seconds which is also the same time the loop force ends

use a delay of 0.1 if you want to accurately track the distance the NPC is from the waypoint

repeat
task.wait(0.1)
local dist = (waypoint.Position - npc:FindFirstChild(“HumanoidRootPart”).Position).Magnitude
if dist <= 1 then – Slightly larger threshold may help too
reached = true
end
until reached or (tick() - startTime) >= 10

I highly recommend you use a pre-made pathfinding module to handle all the pathfinding work for you

it’s both simple to use and optimised for perfomance

1 Like

I used the module u sent and it’s exactly what I needed, thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.