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

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.

i didnt use pathfindingsevices for my old system, heres an explanation of how it worked:
NPC targets a random node (first move)
NPC looks in a folder inside that node, in that folder are object values saying wher it can move next
NPC Puts those nodes it can move to into a table
NPC Chooses a random one
NPC Moves to that random node, then it clears that table and the cycle repeats

2 issues with this system:

  1. NPC couldnt choose any node, then (using other nodes) pathfind to it
  2. NPC would randomly go to nodes that it should not be able to, nor has in its table data

What I would do is use the nodes you have in the folder to generate a graph in code that represents the nodes and their neighbors. You can pass the graph into an A* solver (you’d need to implement this yourself or find an implementation online) and get a path back. From there, you just have the NPC traverse the path.

Doing this would address both issues you had with your old system:

  1. Instead of choosing random nodes, the NPC is following an exact path so it knows which node it needs to walk to next.
  2. Assuming you’ve set up nodes and the object values in the folder correctly, it should be impossible for a path to be calculated that involves moving to nodes that are not neighbors with other nodes.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.