How do you make an NPC that pathfinds but stops at obstructed points?

Let’s say I have an NPC. I want it to pathfind to a destination point that’s completely blocked off. However, I still want it to move through the waypoints, just stop at the waypoint that’s blocked. How can I achieve this? I’m pretty sure with normal pathfinding it will just not allow you to compute the path if it’s blocked.

1 Like

PathfindingService does this automatically and takes into account collisions and whatnot. Just block the path with a part whose CanCollide property is true.

Here’s a tutorial:

I’m not sure you understand. You see, if I have 4 walls around a destination point completely blocking it off, I want it to still pathfind to that destination point and stop moving when it reaches the walls. Basically when it detects that the path is no longer possible.

Oh I see what you mean.

I’m thinking you could create a path that isn’t obstructed, then use the path’s .Blocked event to find out when it was blocked and at which waypoint. Then just stop at that waypoint.

You could also probably just create a table of waypoints for the dummy to move through. This is if you’re doing something more static though. You could cast a ray from the initial point’s direction to the next point’s direction to find the part that is blocking the path and then just use :MoveTo near the part.

I see. That could work, however what if the destination is already blocked off? Should I switch cancollide off for a brief period of time to get the waypoints?

Yeah, I’m thinking you could switch CanCollide to false, call the ComputeAsync and then set it to true after calling it. It’ll automatically “tell” you when the calculations are done since ComputeAsync is asynchronous (it will yield the thread).

part.CanCollide = false
path:ComputeAsync(Vector3.new(0,0,0), Vector3.new(1,1,1))
-- add the .Blocked event here if you are going to go that route
part.CanCollide = true

That would work, however what about the player glitching through walls? It won’t necessarily break anything, it just wouldn’t make sense.

Not sure what you mean. The player can only clip through walls up to 1 stud thick with a laugh clip (assuming they aren’t exploiting). Just make the wall thicker than that (like 2 for example) and they shouldn’t be able to get out.

I mean that turning cancollide off then on for a brief moment of time would potentially cause players being able to glitch through walls.

Oh I see what you mean now, sorry for the confusion haha.

Maybe collision groups could help? You could add the NPC to collision group 1, set the walls to collision group 1, set collision group 1 to not be collidable with group 1, then set all players to group 2, set collision between group 1 and 2 to false. Then after computing, set the NPC’s collision group to the default one. Not 100% sure if pathfinding works with collision groups though.

If it doesn’t you could just have the clients set the wall parts to cancollide = true when modifying the property.

Not too sure how to use collision groups, but I’ll try. Thanks for the help though!

1 Like