[HELP] How can I make am npc pathfinding only walk on a certain path?

Hello,

Im not sure if this is possible without putting barriers but…

How can I make an NPC ignore the grass and only walk on the path its given!?

Here is the script!

local function WalkTo(Position)
	local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("UpperTorso")
	local path = PathfindingService:CreatePath({
		AgentRadius = 2, 
		AgentHeight = 5,
		AgentCanJump = true,
		AgentJumpHeight = 10,
		AgentMaxSlope = 45,
		AgentMaxStepHeight = 0.5,
		AgentAvoidanceEnabled = true, 
		AvoidanceRadius = 15, 
		AvoidanceWeight = 10 
	})

	local startPosition = body.Position
	local endPosition = Position.Position

	path:ComputeAsync(startPosition, endPosition)

	local waypoints = path:GetWaypoints()
	for _, waypoint in ipairs(waypoints) do

		script.Parent.Humanoid:MoveTo(waypoint.Position)
		script.Parent.Humanoid.MoveToFinished:Wait()
	end
	
end

If anyone knows how to do this please let me know! I’ve been stuck on this for hours!

1 Like

Character Pathfinding | Documentation - Roblox Creator Hub is a great resource for learning pathfinding.

I’m not an expert on pathfinding, but it looks like when your WalkTo function is called it collects all the waypoints but isn’t specifying which one of them the NPC is supposed to travel to next.

2 Likes

There are something called Costs in pathfinding. You can use them as so:

local path = PathfindingService:CreatePath({
	Costs = {
		Enum.Material.Grass = math.huge -- I think this would work, I haven't tested it really.
	}
})
2 Likes

That gave an error so I fixed it to this.

Costs = {
		[Enum.Material.Grass] = math.huge 
	}

That didnt really do anything.

After looking through some topics, I see that you should remove the Enum.Material, rather, let it stay as Grass so:

Costs = {
		Grass = math.huge 
	}

If that doesn’t work then you would have to lay down specific parts on the path and use pathfinding modifier and set the cost to a really low number.

1 Like