Need help with Pathfinding

So I am making an RTS sorta game and I need a pathfinding system where you can move NPCs

  1. What I want to achieve: Have a pathfinding system for NPCs (that are ANCHORED at the HumanoidRootPart, I was thinking of moving the character with tweenservice, I just need to find the points) that is no plugin or anything of that sort. I want to know the code
  2. What is the issue?: I have never ever used pathfinding before and have 0 clue how to do it
  3. What solutions have you tried so far?: Looking on the devforum but all I’ve found are pre-made modules and plugins

Any help is appreciated

2 Likes

Is there any reason you want the NPC’s humanoid root part to be anchored?

1 Like

A lot of reasons I can’t really explain too well

Well I’m not sure if pathfinding works with anchored humanoidrootparts - never tested it - it should though considering the arguments are just start position and end position.

But you can just use PathFinding service to get the waypoint positions and then tween it instead of using :MoveTo.

Read this: https://create.roblox.com/docs/characters/pathfinding

You just need to replace :MoveTo with some kind of tween of the humanoidrootpart’s cframe and then change MoveToFinished to detect when the tween is finished instead.

Just some other tips and info:

  • If you’re going to stick with tweening then you can work out the speed of the tween by dividing the magnitude distance between the waypoints by a set speed.

  • Make sure you handle the possibilit that the TweenFinished might never be called, maybe add some kind of maximum time before it moves onto the next waypoint.

  • I would still advise you to just use unanchored humanoid root part’s to preserve animations since if you anchor the humanoidrootpart then it will just move with 0 animations. If you just want it to be controlled by the server (by anchoring it) then you should just set the NetworkOwnership of all the NPC’s baseparts to null (to the server) and keep it unanchored.
1 Like

Okay then I will make it unanchored. Is there a way for it to stay on tiles and not walk diagonally since it the floor is a bunch of tiles? Also I want it to stay away from tiles that have a value set to true.

You should look into A* pathfinding, or actually ditch roblox’s pathfinding algorithms and look at other custom pathfinding algortihms considering you are going for tiles.

Do you know how to do that? I got 0 clue how to do it and don’t really understand A*

A* is a pathfinding algorithm that finds the shortest distance from a destination to a target point. You said you were using tiles so it would work well for this.
Its efficiency comes from a thing called a heuristic, which is gives the algorithm a rough idea of a path to take, which guides the algorithm towards the target.

This is a A* I found written in lua, try customising it to work in studio.

You could just do that with a PathfindingModifier and setting its cost to math.huge

How do you use PathfindingModifier, like I said I got 0 clue what anything pathfinding related is right now

I just want to find out how to make the pathfinding points for the grid

As you may know, pathfinding uses something called a “Navigation Mesh” which is automatically generated in your experience, each face on it contains material information based on the closest part relative to that face. When creating a path with PathfindingService:CreatePath() you can define a table within the brackets with info about your character like if it can jump, its height and radius, and material costs which are important in relation to PathfindingModifiers

PathfindingModifers can be used to change the context of a part through the eyes of a pathfinding NPC, like making the NPC think a part has no collision when it does with the PassThrough boolean. You can also set the Label string value in a PathfindingModifier to something like “pathblocker” then in the CreatePath() function, define that Label as a Material cost

Example with a pathblocker PathfindingModifer used to have an NPC avoid an area:

local PathfindingService = game:GetService("PathfindingService")

local path = PathfindingService:CreatePath({
   Costs = {
       pathblocker = math.huge,
       Neon = 100,
   }
})

Note that Costs or Material cost just tells the NPC how easy a material is to walk on. The lower the number is the easier it is which means that material will be walked on in favor of other, harder to walk on materials.

math.huge generates the biggest possible number in Roblox, which results in the NPC avoiding it as much as possible.

TL;DR
Put a PathfindingModifier in a part and set its label string value to whatever makes the most sense and when creating a Humanoid based characters pathfinding, reference it in the Costs.

If you wanna learn more I recommend reading this:

and this