How can I make my path generator avoid water?

I have a path generator that uses Bezier curves. it generates fine but when there’s water in the path of the road it just cuts off.
How can I make the path generator generate paths around the edges of the water?

local module = {}
function getPascal(size)
	local row = {1}
	if size > 1 then
		for i = 2,size do
			local newrow = {}
			for x = 1,i do
				if x == 1 or x == i then
					newrow[x] = 1
				else
					newrow[x] = row[x-1] + row[x]
				end
			end
			row = newrow
		end
	end

	return row
end


function getPosition(t, perc)
	local inv = (1-perc)
	local result = Vector3.new(0,2500,0)
	local SIZE = #t
	local pasc = getPascal(SIZE)

	for i, pos in pairs(t) do
		result = result + pos * inv^(SIZE - i) * perc^(i-1) * pasc[i]
	end
	return result
end
function module:Generate(A,B,ammount,roadsegments,offsetammounts)
	local t = {A}
	local raycastparams = RaycastParams.new()
	raycastparams.FilterDescendantsInstances = {workspace.Terrain}
	raycastparams.FilterType = Enum.RaycastFilterType.Whitelist
	for i = 1, roadsegments do
		local newPos = A + (B - A).Unit * i * (B - A).Magnitude / roadsegments
		local offset = Vector3.new(math.random(-offsetammounts, offsetammounts), 0, math.random(-offsetammounts,offsetammounts))
		if i == 0 or i == roadsegments then
			offset = Vector3.new(0, 0, 0)
		end
		newPos = newPos + offset
		table.insert(t, newPos)
	end
	for i = 0,ammount do
		local ray = workspace:Raycast(getPosition(t, i/ammount), Vector3.new(0,-5000,0), raycastparams)
		if ray then
				local min = ray.Position - (0.5 * Vector3.new(10,50,10))
				local max = ray.Position + (0.5 * Vector3.new(10,50,10))
				local region = Region3.new(min, max)
				workspace.Terrain:ReplaceMaterial(regi	on, 4, Enum.Material.Grass, Enum.Material.Mud)
				workspace.Terrain:ReplaceMaterial(region, 4, Enum.Material.Snow, Enum.Material.Mud)
				workspace.Terrain:ReplaceMaterial(region, 4, Enum.Material.Rock, Enum.Material.Mud)
				workspace.Terrain:ReplaceMaterial(region, 4, Enum.Material.Sand, Enum.Material.Mud)
		end
	end
end
return module

Maybe you can use pathfinding to find a way past nodes that are located at Y < WATER_LEVEL.

tho how would i make the path go around the water?

When you raycast, it returns a material hit if possible. Check if that material was Enum.Material.Water.

You can use an invisible part/wall around the water and make the code detect that block so it doesnt go near it.

  1. Run the pathfinding algorithm
  2. Raycast in-between the resulting path nodes
  3. Replace the found material with your path material

i mean it would look weird if it just cuts across a big body of water…

How would i make it avoid it tho, if i were to just not spawn a road there it would just end on one side then start of the other. I dont really want that…

raycastparams.IgnoreWater = false

i think this is what youre looking for

not really as i said before

i dont really want that ._.

oh, sorry i did not see that. my bad