How do i make node based pathfinding; is it hard?

i want to make a pathfindign script for my game to have NPCs; this is how i want it to work:

Lets say you have 3 symbols, and one is a NPC:

@    @

.    @

the NPC want to go to the top left, but instead of just jumping there, i want it to pathfind like this:

@    @

.    @
.   @

@   @
@   .

@   @

if you understand please answer this:
How hard would it be to make this for a semi new scripter? and how would i do it?

Are you wanting to pre-define these nodes yourself?

1 Like

the nodes are going to be in a folder
and the nodes will be parts

Should be easy enough. You can play around with the code to your liking.

This is an example implementation. Place this inside of the character you want to move around the nodes. Make sure you have a folder in workspace with your nodes.

local pathfindingService = game:GetService("PathfindingService")
local nodesFolder = game.Workspace:WaitForChild("Nodes")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local function moveToNode(node)
    local path = pathfindingService:CreatePath({
        AgentRadius = 2,
        AgentHeight = 5,
        AgentCanJump = true,
        AgentJumpHeight = 5,
        AgentMaxSlope = 45,
    })
    
    path:ComputeAsync(character.PrimaryPart.Position, node.Position)
    
    local waypoints = path:GetWaypoints()
    
    if path.Status == Enum.PathStatus.Complete then
        for _, waypoint in ipairs(waypoints) do
            if waypoint.Action == Enum.PathWaypointAction.Jump then
                humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
            end
            humanoid:MoveTo(waypoint.Position)
            humanoid.MoveToFinished:Wait()
        end
    else
        warn("Pathfinding failed!")
    end
end

local function moveToNodesSequentially()
    for _, node in ipairs(nodesFolder:GetChildren()) do
        if node:IsA("BasePart") then
            moveToNode(node)
        end
    end
end

moveToNodesSequentially()

1 Like

sorry i was busy, ill try it thanks!

TBH, i didnt even know there wa a pathfinding service

Im getting erros, first one is “Complete is not a valid member of “Enum.PathStatus” - Server - Test:19”

NVM, i was able to just change it to “success” instead, it works thank you!

1 Like

NEVERMIND IM SORRY, im doing a bigger scale test and its jumping to other nodes now

1 Like

Did you want it to jump to a node in a specified order?

Heres a dumb little drawing i made for you:
image

image
OR
image

1 Like

it has already got a module to use the pathfinding and a plugin to edit the nodes.

1 Like

i dont want to use a plugin, i want to just put parts in a folder and have this NPC go somewhere on the nodemap, following the avalible path

Unfortunately this falls out of my knowledge scope, but hopefully someone else has the solution. I am not super experienced in setting my own pre-defined nodes and restricting them like that.

1 Like

You can implement your own version of A* pathfinding for this. There are many useful guides on how to do so online. This video is a great starting point in my opinion. The exact details on how A* should be implemented depend on the needs of your project.

1 Like

To add onto what @player356377 said, to achieve the particular movement you showed in your pictures using A*, you can just set up the nodes and the edges such that each node has no diagonal neighbors. I edited your original image to show what the edges would be (in white lines)

NodeGraph

thats the exact way i made mine before, (i did try to make node based path finding)
For example. if the NPC was in the top left, it would only be given two options:
Left or Down
but even with thys system, it would just move to random point, plus it had to reach each point then it made a decision, instead of deciding to go to another node and using the avalible nope path to go to it

What do you mean it would move to a random point? It would move diagonally or it would move to some non-node point?

Also, what do you mean it would decide after each node? That sounds like it might be a problem with how you implemented the actual traversing. I’ve never experienced an issue like that.

Look at this post i made for another person

If it’s jumping to other nodes, then you have not set up the edges/neighbors correctly. If you’re using PathFindingService, then I can’t give much input on that since I haven’t really messed with it. I expect that you wouldn’t be able to get your desired behavior using it.

As far as I know, there’s no way tell that service that you want to use your own custom nodes. It uses some internal navmesh that it generates and I don’t know to what extent you can customize that.