How to make pathfind avoid terrain

so i have a npc and sometimes it randomly starts swimming in the ocean ( sand under the ocean ), so how would i make it so the pathfinding avoids terrain, heres the part of the script that makes it move:

function pathToLocation(location)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myRoot.Position,location)
	local waypoints = path:GetWaypoints()
	pathCount = pathCount + 1

	if path.Status == Enum.PathStatus.Success then
		local currentPathCount = pathCount
		for i, waypoint in ipairs(waypoints) do
			if currentPathCount ~= pathCount then
				return
			end
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			myHuman:MoveTo(waypoint.Position)
			delay(0.5, function()
				if myHuman.WalkToPoint.Y > myRoot.Position.Y then
					myHuman.Jump = true
				end
			end)
			local moveSuccess = myHuman.MoveToFinished:Wait()
			if not moveSuccess then
				break
			end
			if fleeing == true then
				if i == #waypoints then
					wander()
				end
			end
		end
	else
		getUnstuck()
	end
end

Use PathfindingModifiers. You can specify terrain that the NPC should avoid by linking certain materials to costs.

This Roblox Creator Guide gives an explanation on how to do this.

1 Like

for cost number would i set it to math.huge if i dont want it to go there at all

Yeah

1 Like