Getting Pathfinding to go across the green blocks

Hello, my pathfinding is working fine, however, it cuts corners. Right now I have the cost of the concrete (the road) to 0.1, and the cost of Fabric (the blocks material) to 0.01. When going to the waypoint, it cuts corners, even though it stays on the road. I want it to follow the green blocks instead to get to its end destination. How can I do this?

local AgentParameters = {
			WaypointSpacing = 4,
			Costs = {
				Fabric = 0.01,
				Concrete = 0.1
				
			}
		}

4 Likes

Set your scripts to follow those green blocks. Give each block a name like:

Block1
Block2
Block3
Block4

Then you can run a loop that goes each of the blocks that the guy is going to go.

Idk if u already did that

2 Likes

The end destination is picked at random so that wouldn’t work.

1 Like

So the end destination would be one of the blocks above?

1 Like

Correct, and I want it to follow the blocks that are on the way to the end block

1 Like

The easier approach is to resize the road to the width of the block and make it invisible and change it into a different material with a lower cost.

1 Like

I have tried this, but it doesn’t work for transparent parts I found out

1 Like

Sooooooo is the starting destination the same? And also does your path change or stay the same with the blocks in the same position?

1 Like

Starting point isn’t the same, and the path doesn’t change but I want to be able to add more paths freely.

1 Like

You want to add more paths like a tree?

1 Like

No? sdf sdfsdfsdfsdfsdfsdf ffsdsdf

2 Likes

I apologize for the mix-up. I’m a little sleepy today. Soooooo are you want to add more paths freely in a way that the path is still a line?

2 Likes

Yes sdfjsdklfjsdfklsdklfsdfsdf

2 Likes

Meaning that I don’t want it to be hard coded.

2 Likes

Sorry, don’t report me for this bump. I just really need help.

1 Like

What are you using for path finding. You talk about waypoint spacing and cost, what code are you using that will need these values in relation to the pathfinding?

1 Like

Would you like me to send the entire script?

1 Like
local PathFindingService = game:GetService("PathfindingService")

local NODES = game:GetService("Workspace").NODES

local pathfinding = {}

pathfinding.Path = function(npc)
	local randomNode = nil
	local success = false
	local pathIsSuccessful = false
	
	repeat 
		randomNode = NODES:GetChildren()[math.random(1, #NODES:GetChildren())]
		local partsIn = game.Workspace:GetPartsInPart(randomNode)
		for i, v in pairs(partsIn) do
			if v.Parent == npc then
				success = false
				continue
			end
		end

		success = true
	until success == true
	
	local function walk()
		
		local path:Path
		local AgentParameters = {
			WaypointSpacing = 4,
			Costs = {
				Fabric = 0.01,
				Concrete = 0.1
				
			}
		}
		path = PathFindingService:CreatePath(AgentParameters)
			
		local humanoidRootPart = npc:WaitForChild("HumanoidRootPart")
		
		local connection 
		local pathBlockedConnection

		local RETRY_NUM = 0
		local success,errormessage

		repeat 
			RETRY_NUM += 1
			success, errormessage = pcall(path.ComputeAsync, path, humanoidRootPart.Position, randomNode.Position)
			if not success then 
				task.wait(.5)
			end
		until success or RETRY_NUM > 5

		if success then
			if path.Status == Enum.PathStatus.Success then
				local waypoints = path:GetWaypoints()
				local currentIndex = 2

				if not connection then
					connection = npc.Humanoid.MoveToFinished:Connect(function(reached)
						if reached and currentIndex < #waypoints then
							currentIndex += 1
							npc.Humanoid:MoveTo(waypoints[currentIndex].Position)
							if waypoints[currentIndex].Action == Enum.PathWaypointAction.Jump then
								npc.Humanoid.Jump = true
							end
						else
							print("Disconnection connection")
							connection:Disconnect()
							connection = nil
							pathBlockedConnection:Disconnect()
							pathBlockedConnection = nil
							pathIsSuccessful = true
						end
					end)

					pathBlockedConnection = path.Blocked:Connect(function(waypointNumber)
						if waypointNumber > currentIndex then
							print("Disconnection path block")
							connection:Disconnect()
							pathBlockedConnection:Disconnect()
							connection = nil
							pathBlockedConnection = nil
							walk()
						end
					end)
				else
					print("Connection exists")
				end
				
				npc.Humanoid:MoveTo(waypoints[currentIndex].Position)
				
				if waypoints[currentIndex].Action == Enum.PathWaypointAction.Jump then
					npc.Humanoid.Jump = true
				end
			else
				warn("Path was not a success")
			end
		else
			warn("Path computing error", errormessage)
			return
		end
	end
	
	walk()
	repeat task.wait(.2) until pathIsSuccessful == true
	return pathIsSuccessful
end

return pathfinding

I will just send it either way

1 Like

Also, as a second question, is there a way to stop the :MoveTo() method? (Just stop the npc in its place, therefore cancelling the pathfinding)

1 Like

What are you doing in this part of code?
image

1 Like