I need help with my pathfinding code

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

the code im using is not working at all

  1. What is the issue? Include screenshots / videos if possible!
    its giving me a warning in the output ( yes i did do that)
  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    heres my code:
    i tried looking on the developer forums but found nothing that could fix my issue

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local pfs = game:GetService("PathfindingService")

script.Parent.AncestryChanged:Connect(function()
	if (script.Parent.Parent.Name == "Mobs") then
		local player = game:GetService("Players"):FindFirstChild(script.Parent.Parent.Parent.Owner.Value)

		if (player) then

			local base = workspace.Bases[player.Base.Value]

			local path:Path =  pfs:CreatePath()
			path:ComputeAsync(script.Parent.HumanoidRootPart.Position,base["Village Hall"].PrimaryPart.Position)
			if(path.Status == Enum.PathStatus.Success) then
				for index,wp in pairs(path:GetWaypoints()) do
					script.Parent.Humanoid:MoveTo(wp.Position)
					script.Parent.Humanoid.MoveToFinished:Wait()

					local point = Instance.new("Part")
					point.Material = Enum.Material.Neon
					point.Position = wp.Position
					point.Anchored = true
					point.CanCollide = false
					point.Parent = workspace

					if (wp.Action == Enum.PathWaypointAction.Jump) then
						script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
					end
				end
			elseif (path.Status == Enum.PathStatus.NoPath) then
				warn("could not compute path!")
			end
		end
	end
end)	

thanks for reading. if you can help me then please do it.

I also added a few parts on the waypoints position to know where its going

What is the warning you’re getting in the output?


Also make sure to fix this.

“could not compute path!” is the warning as i have done in the above code. And what do you mean by “fix this” theres no syntax error

Could you show a visual of the layout of workspace including the position of your character and the goal part.

How is that in any way related to the warning in my code? The goal part is in mid air (around the same height as the humanoidrootpart)

That means the goal is unreachable. To make sure the humanoid is able to reach the goal, go into Studio settings and enable “Show Navigation Mesh."

image


Arrows between parts mean that pathfinding is possible.

image

1 Like

its not showing the arrows even though it should be. is this because it is a model?

It’s not showing because it’s not traversable. The Humanoid cannot just go anywhere you want it to go unless walking/jumping to that position is possible.

1 Like

walking and jumping to that place is possible, even for a player. the goal is blocked by a wall, but you can still reach your destination. i see no reason pathfinding is not possible

You can very well reach the goal using your character but there are certain limitations to Roblox’s PathfindingService which prevent the Humanoid from reaching the goal even if you are able to. The only solution to your problem is either to create a custom pathfinding algorithm instead of using PathfindingService or if you want to keep using PathfindingService, make sure you move your parts so that all of them are connected by arrows.

thanks ill try it. but making a pathfinding algorithm is quite hard. ill assume youll have to make a while loop, then check if pathfinding is possible using raycasting or use a for loop that loops through the entire game and spawns a few parts and check which is the best path possible.

Just make it

local path = pfs:CreatePath()

Only if the normal roblox player can jump to that position, then npc can jump, however by default npc cannot jump high, you need to edit the settings of path finding when finding path to detect blocks higher, however npc cannot jump higher than normal player if you keep the humanoid setting as low jump power

I tried that but it didnt work