what I’m trying to achieve - making a npc that chases a player and doesn’t get stuck on walls and small obstacles.
I tried making a npc that chases a certain player but it keeps getting stuck on walls and small obstacles
the issue is pathfinding is really expensive on performance apparently so I never tried it
even then if I wanted to use pathfinding I don’t know how to implement it to a while loop
This documentation gives you usage details on Character Pathfinding. Upon review, let me know if any clarification is needed. I don’t think it is highly expensive on performance as ROBLOX would’ve taken that factor into mind.
First of all, there are MANY posts/articles already on this topic, did you not find them by simply googling for them before making this post?
Second, pathfinding on Roblox is generally pretty painful to deal with. Roblox’s pathfinding service works well when you don’t have many NPCs and that don’t require constantly re-computing. It’s a good starting point but you will likely find you have to make your game fit the pathfinding you have available, rather than trying to make the pathfinding work for your game (if that makes sense).
For example, you might just move the enemy towards the player when they are close enough without any pathfinding (like what you have basically), so that its not having to constantly recompute new paths.
This then means you want to design the areas that your game has NPCs as easily traversable (think like a dungeon room, flat, nothing for the NPCs to get stuck on). I’ve made many NPC systems on roblox (both using pathfinding service and custom) and honestly I recommend just making your environments that your NPCs are in as simple as possible to avoid the headaches that come with trying to work with pathfinding.
Often on Roblox when people try to create NPCs, they try make some sort of “one size fits all” NPC that they hope can basically traverse any environment and do anything they want. This is not a good approach because it is 100x harder than just creating NPCs for a specific environment.
You want to avoid constantly computing new paths if you can as this is expensive for the server.
The alternative is to use A* which is a pathfinding algorithm (its what roblox’s one uses under the hood alongside a navigation mesh).
The reason its more efficient is because you manually place ALL the nodes that can possibly be used for pathfinding which means there tends to be far fewer nodes than roblox’s navigation mesh and it can therefore run a lot faster (I’ve found it to be over 100x faster in my use cases).
There are some big downsides. Because you are pre-placing all the nodes it means that if your map changes, the nodes wont reflect that. It’s best for static maps (like maps where nothing changes).
It also can take some time to place all the nodes depending on your map size (crossroads would take around 25-30 mins to do).
It also requires a lot more work to setup and get going, which is the main reason that even though I’ve talked about it I strongly recommend staying away unless you are prepared to dedicate weeks to this and are very confident in your roblox lua skills.
Can you tell me about your game and where the NPCs need to be? As I said its far better if you can adapt your game to minimize needing pathfinding than it is to try come up with a solution.
If your game has a lot of mazes and you want to have NPCs, then you are definitely going to need pathfinding.
Are the maps static? Like does the maze change?
In your case, I would go with A* manual nodes, and then if the NPC has line of sight (simple raycast check), it just ignores pathfinding and heads straight for them.
If you have things like ledges and cliffs, it makes it a lot harder as you then have to try account for edge detection which atp you should just use Roblox’s pathfinding and bite the bullet.
One thing you should 100% do is have a test maze, use roblox pathfinding and just see how it runs. If it ends up running perfectly fine for what you need then there is no reason to do the harder option.
In the case of a maze, I feel the NPC will get stuck so often it won’t make much difference.
If you really want to try, then to detect if an NPC is stuck you could just check if they are meant to be chasing and if they are moving. So if an NPC has a target, but say hasn’t moved more than 2 studs from their current position in over 2 seconds, you could count that as “stuck” and then re-compute.
Another thing you should keep in mind with your game is memory/line of sight for the NPCs. For example, if an NPC sees a player, but then that player hides behind a wall, it should still follow the NPC to where it last saw them. But if a player is close to an NPC but the NPC can’t see them (e.g. player is behind a wall), the NPC shouldn’t still chase them.
I think that Pathfinding Service is great if you use it efficiently. I fail to comprehend the issues you mention such as it being expensive on the server unless you are re-calculating too much, which should not be the case so long as you add appropriate configurations and properties to consider object costs, jumping radius etc.
I think it is rather misleading to promote using “A* which is a pathfinding algorithm” when it is already the backbone of Pathfinding Service on Roblox. The service is optimised for games so long as you are using best practices and are not writing sloppy code.
Hope this redirects you away from custom use-cases unless required @ayyansinan_22.
In regards to SimplePath, I think that module is decent but you can always make your own with the roblox service instead of a custom a* algo solution.
PathfindingService is not good if you have many NPCs that need to be updated constantly. For example, if you have 20 enemy sword NPCs that need to traverse an environment with a lot of obstacles to chase and attack players.
I think you misunderstand. Yes Roblox’s PathfindingService uses A*, but its using it with a navigation mesh which is basically thousands of nodes (even with a lot of optimizations), its designed to be an easy one size fits all solution and in many cases it works well but not all, which is why I asked OP what his game was for.
Er? SimplePath is just roblox’s pathfindingService under the hood.
I’ll gladly retract what I’ve said if you can show me 20+ npcs that can effectively traverse a difficult environment, while constantly updating to find new targets, while not slowing down the server considerably.
I do not want this to be a back and forth so I suppose to begin we should - agree to disagree on some points just for ease.
I’m very much understood on ROBLOX pathfinding, but your example is not what the OP is asking for which is an NPC chasing a Player. You have an example of 20 NPCs in an environment with high levels of obstacles and actions - which I have successfully achieved personally in a professional game.
There are alternatives to things such as MoveToFinish which cause stuttering and poor performance for example, so I think it can very much depend on the ways you approach these tasks.
I did not deny this and was simply suggesting if it did not fit their use case they can always make their own module.
Hope that clarifies.
Finally, I’m aware of Tyridge’s approach to yes, and concur there are cases where PathfindingService is not the best approach. I am under the belief from OP’s info that it is satisfactory. Note that the Help and Feedback > Scripting Support category is to address individual issues, and this one I fear lacks the need for a custom algo.
I hope this clears it up from my perspective and won’t be engaging in this thread further, but feel free to DM me if you want any further convos on this subject.
Can you show this then? As I would be very interested.
Sure but I am still going to reply to your last points.
I already addressed this in my first response to OP, where I clearly said that:
OP’s case requires constantly updating the pathfinding as it is probably the worst possible case for pathfinding, a maze, which will be unusable for NPCs without pathfinding. OP said “npcs”, meaning more than 1. Because of this I suggested looking at manual nodes since he now fits the category of wanting multiple NPCs with constant pathfinding updating.
The other thing I said to OP was:
I feel that you haven’t read my previous replies very well and that is leading us to our current problems.
but you know what would really be good non humanoid npc’s I want to try making those in the future because apperantly humanoids are heavy on performance I could be wrong about that though.