Hiya there! I’m currently trying right now to make a NPC pathfind through a maze(free model for testing), but currently, the NPC just refuses to move or create waypoints. The solution I tried(pretty new scripter) was to replace the _ in _,waypoint with i, but that didn’t help. Please help!(no errors in output either)
Code:
local pathfindService = game:GetService("PathfindingService")
local path = pathfindService:CreatePath()
local npc =script.Parent
local goal = workspace.Goal
local humanoid = npc.Humanoid
path:ComputeAsync(npc.HumanoidRootPart.Position,goal.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Poition
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
That might be the problem if it’s failing to compute it somehow. Or the path might be blocked from the start.
If that doesn’t bring anything up make sure the position of the start and goal are spread out from each other. And maybe even try replacing the start(HumanoidRootPart) with just a normal part inside workspace.
I did that(right before the for_,waypoint) and it says there isn’t a path. Still no errors. Edit: I just realized, If I move it into the for _, waypoint thing, it doesn’t print the status.
Ok well then you are either already at the destination from the start, the path is being blocked by a wall at the start, or there is a jump the npc needs to jump over, like a hole in the ground. If it’s not printing anything in the waypoint loop, there is either no way points or the path failed to compute.
Hmm, that’s weird. It must not be returning anything because the path failed to compute.
Try something like this:
local pathfindService = game:GetService("PathfindingService")
local path = pathfindService:CreatePath()
local npc =script.Parent
local goal = workspace.Goal
local humanoid = npc.Humanoid
path:ComputeAsync(npc.HumanoidRootPart.Position,goal.Position)
if path.Status == Enum.PathStatus.Success then
-- Get path waypoints
local waypoints = path:GetWaypoints()
-- Loop through waypoints
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Poition
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
print("PATH FAILED! Reason: "..tostring(path.Status))
end
So I looked through it and that is one big maze. There must be something along the way that is causing it to break. Try this: maze pathfinding testfix.rbxl (55.9 KB)
It basically goes through checkpoints at a time instead of finding the whole path at the beginning. You should be able to figure out what to do when you run this.