Connecting two points on a grid

I am trying to connect two tiles in a grid I have made to make a river, however, the simple connection script I wrote passes right by the finish point, and even goes in the wrong direction sometimes.

In this picture, you can see how it goes down before going up.
image

Here is the entirety of my code, I believe the problem comes from the connect function, but if you find any other issues or ways my code could be improved please tell me!
Thank you!

P.S the unused points at the bottom of the script will be used later, please don’t mind those.

local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage:WaitForChild("StartGame")
local createGUI = replicatedStorage:WaitForChild("CreateGUI")
local tilesToReplicate = replicatedStorage:WaitForChild("Tiles")
local grassTile = tilesToReplicate:WaitForChild("GrassTile")
local waterTile = tilesToReplicate:WaitForChild("WaterTile")
local sandTile = tilesToReplicate:WaitForChild("SandTile")
local resetGUIConnection
local resetGUIConnection2
event.Event:Connect(function(players,sizeOfGrid,position,mapType,water)
	local tiles = {}
	local rowCol = {Row=1,Column=1}
	local newTile
	local tileFolder = Instance.new("Folder",workspace)
	tileFolder.Name = ("TileFolder")
	local function replaceTile(tileToReplace,newOne)
		local row = tonumber(tileToReplace.Name:sub(1,2))
		local column = tonumber(tileToReplace.Name:sub(4,5))
		local number = column*sizeOfGrid+row
		local newPos = tileToReplace.PrimaryPart.CFrame
		tileToReplace:Destroy()
		tileToReplace = newOne:Clone()
		tiles[number] = tileToReplace
		tileToReplace.Parent = tileFolder
		tileToReplace:SetPrimaryPartCFrame(newPos)
		if row >= 10 and column >= 10 then tileToReplace.Name = (tostring(row)..":"..tostring(column))
		elseif row >=10 then tileToReplace.Name = (tostring(row)..":0"..tostring(column))
		elseif column >= 10 then tileToReplace.Name = ("0"..tostring(row)..":"..tostring(column))
		else tileToReplace.Name = ("0"..tostring(row)..":0"..tostring(column))	
		end
	end
	local function connectTiles(tileOne,tileTwo,tileType)
		local tileOneRow = tonumber(tileOne.Name:sub(1,2))
		local tileTwoRow = tonumber(tileTwo.Name:sub(1,2))
		local tileOneColumn = tonumber(tileOne.Name:sub(4,5))
		local tileTwoColumn = tonumber(tileTwo.Name:sub(4,5))
		local distanceX = math.abs(tileOneRow-tileTwoRow)
		local distanceY = math.abs(tileOneColumn-tileTwoColumn)
		local directionX = distanceX/(tileOneRow-tileTwoRow)
		local directionY = distanceY/(tileOneColumn-tileTwoColumn)
		replaceTile(tileOne,tileType)
		local currentTile
		local currentTileNumber = (tileOneColumn*sizeOfGrid+tileOneRow)
		while currentTile ~= tileTwo do
			if distanceX < distanceY then
				currentTileNumber = currentTileNumber-(directionY*sizeOfGrid)
				distanceY = distanceY-1
			elseif distanceX > distanceY then
				currentTileNumber = currentTileNumber-(directionX)
				distanceX = distanceX-1
			else
				if math.random(1,2) == 1 then
					currentTileNumber = currentTileNumber-(directionX)
					distanceX = distanceX-1
				else
					currentTileNumber = currentTileNumber-(directionY*sizeOfGrid)
					distanceY = distanceY-1
				end
			end
			currentTile = tiles[currentTileNumber]
			replaceTile(currentTile,tileType)
		end
	end
	for i=1,sizeOfGrid^2 do
		--randomize this part later for the map gen
		local tile
		if mapType == "Random" then
			tile = tilesToReplicate:GetChildren()[math.random(1,#tilesToReplicate:GetChildren())]
		else
			tile = mapType
		end
		newTile = tile:Clone()
		newTile.Parent = tileFolder
		newTile:SetPrimaryPartCFrame(CFrame.new(Vector3.new(position.X + (rowCol.Row*8),position.Y,position.Z + (rowCol.Column*8))))
		table.insert(tiles,newTile)
		if rowCol.Row >= 10 and rowCol.Column >= 10 then newTile.Name = (tostring(rowCol.Row)..":"..tostring(rowCol.Column))
		elseif rowCol.Row >=10 then newTile.Name = (tostring(rowCol.Row)..":0"..tostring(rowCol.Column))
		elseif rowCol.Column >= 10 then newTile.Name = ("0"..tostring(rowCol.Row)..":"..tostring(rowCol.Column))
		else newTile.Name = ("0"..tostring(rowCol.Row)..":0"..tostring(rowCol.Column))	
		end
		if rowCol.Row == sizeOfGrid then
			rowCol.Column = rowCol.Column + 1
			rowCol.Row = 1
		else
			rowCol.Row = rowCol.Row + 1
		end
	end
	if water == "Lake" then
		local notZero = 0
		while notZero == 0 or notZero == 1 or notZero == -1 do
			notZero = math.random(math.floor(-sizeOfGrid/5),math.floor(sizeOfGrid/5))
		end
		local numberOfTile = math.floor(#tiles/2)+(math.random(math.floor(-sizeOfGrid/5),math.floor(sizeOfGrid/5))+notZero*sizeOfGrid)
		local aPointTile = tiles[numberOfTile]
		local xdivergence = (math.floor(sizeOfGrid/2)-tonumber(aPointTile.Name:sub(4,5)))
		local ydivergence = (math.floor(sizeOfGrid/2)-tonumber(aPointTile.Name:sub(1,2)))
		local bPointTile = tiles[(numberOfTile + xdivergence*sizeOfGrid*2)+(math.random(-1,1)*sizeOfGrid)]
		local cPointTile = tiles[(numberOfTile + xdivergence*sizeOfGrid)+math.floor(ydivergence*(math.random(7,10)/10))]
		local dPointTile = tiles[(numberOfTile + xdivergence*sizeOfGrid) - math.floor(sizeOfGrid-ydivergence+(math.abs(ydivergence)/ydivergence))]
		local keys = {aPointTile,bPointTile,cPointTile,dPointTile}
		bPointTile.Grass.BrickColor = BrickColor.Red()
		cPointTile.Grass.BrickColor = BrickColor.Gray()
		connectTiles(bPointTile,cPointTile,waterTile)
	end
	for _,player in pairs(players) do
		createGUI:FireClient(player,tiles,"Add")
		resetGUIConnection = player.CharacterAdded:Connect(function()
			wait(2)
			createGUI:FireClient(player,tiles,"Remove")
			wait(0.1)
			createGUI:FireClient(player,tiles,"Add")
		end)
	end
end)

while I haven’t read your script, the logic is as follows at each iteration (pseudo code):

if start.X > current.X then 
 nextX = current.X + 1
elseif  start.X < current.X then 
 nextX = current.X - 1
end

Same logic for Z. Do nothing if it is “=”, only “>” or “<” At the end, check if current X and Z are equal to end X and Z. If it is, then you’ve reached the end and terminate the loop.

I guess you called X and Z rows and columns.

1 Like

I’m going to completely redo the script to work with 3d noise. Thank you for the help though.