Gaps in Neighbouring polygons (Procedural mesh component)

Hello

I am trying to create some mesh through the Procedural mesh component in C++, and the neighbouring polygons have diffrent Z values. The intended effect is that the neighbouring vertices should have the same Z value. But i am not succesful and this is the result:

Have been trying for some time to get this working, and i have been using these two tutorials as the main references:

[SiggiG github][2]

[wlosok][3]

I have made many attempts to make the poygons on first row inherent the Z value from the neighbouring vertices. Thinking about creating the polygons with sharing vertices, but that would be take some time for me to wrapp my head around how the Triangels would be added.
I am fairly new to c++ and if anyone have any suggestions on how i should approch this then i would really appreciate it.

This is the code (very much WIP):

void ACustomTerrain::GenerateTerrain(int32 TS, int32 XCurrentPos, int32 YCurrentPos, int32 LeftRandomZ1, int32 RightRandomZ2, int32 LeftRandomZ3, int32 RightRandomZ4)
{

corners 

	Vertices.Add(FVector((XCurrentPos*TS)     , (YCurrentPos*TS + TS), LeftRandomZ1));
	Vertices.Add(FVector((XCurrentPos*TS + TS), (YCurrentPos*TS + TS), RightRandomZ2));   	 
    Vertices.Add(FVector((XCurrentPos*TS)     ,	(YCurrentPos*TS), LeftRandomZ3));		
	Vertices.Add(FVector((XCurrentPos*TS + TS), (YCurrentPos*TS), RightRandomZ4));		


	// TODO Correct the commets
	Triangles.Add(VertexIndex);		//Upper left - 0
	Triangles.Add(VertexIndex + 1);	//Upper right - 1
	Triangles.Add(VertexIndex + 3);	//Bottom right - 3


	Triangles.Add(VertexIndex + 2);	//Bottom left - 2
	Triangles.Add(VertexIndex);		//Upper left - 0
	Triangles.Add(VertexIndex + 3);	//Bottom right - 3

	VertexIndex = VertexIndex + 4;

}


void ACustomTerrain::GenerateCubeMesh()
{
	TArray<FLinearColor> VertexColors;

	int32 ZVal1;
	int32 ZVal2;
	int32 ZVal3;
	int32 ZVal4;

	int32 NumberOfVerts = 0;

// init for the height value
// TODO 
	for (int32 y = 0; y < YTileCount; y++)
	{

		//Loop for creating X
		for (int32 i = 0; i < XTileCount; i++)
		{
		
			for (int32 g = 0; g < 5; g++) 
			{
				ZIndex.Add(rand() % 20 + 1);
			};

		};
	};

	//Loop for creating Y
	for (int32 y = 0; y < YTileCount; y++)
	{

		//Loop for creating X, creating tiles towards left of the center
		for (int32 x = 0; x < XTileCount; x++)
		{
			NumberOfVerts = NumberOfVerts + 4;
			// First poly
			if ((x == 0) && (y == 0))
			{
				ZVal1 = ZIndex[0];
				ZVal2 = ZIndex[1];
				ZVal3 = ZIndex[2];
				ZVal4 = ZIndex[3];
			}
			else
			{
				if ((y == 0) && (x > 0))
				{
					// only the first column

					ZVal1 = ZIndex[NumberOfVerts - 7];	//Upper right  - 0
					ZVal2 = ZIndex[NumberOfVerts - 3];	//Upper left   - 1
					ZVal3 = ZIndex[NumberOfVerts - 2];	//Bottom right - 2
					ZVal4 = ZIndex[NumberOfVerts - 1];	//Bottom left  - 3

				}
				else
				{
					// The rest of the columns
					ZVal1 = ZIndex[NumberOfVerts - 6];	
					ZVal2 = ZIndex[NumberOfVerts - 5];	
					ZVal3 = ZIndex[NumberOfVerts - ((XTileCount*4)-2)];	
					ZVal4 = ZIndex[NumberOfVerts - ((XTileCount*4)-1)];
				};

			};

			GenerateTerrain(TerrainSize, x, y,ZVal1, ZVal2, ZVal3, ZVal4);


			VertexColors.Add(FLinearColor(1.0f, 1.0f, 1.0f));	//Upper left
			VertexColors.Add(FLinearColor(1.0f, 0.0f, 0.0f));	//Upper right
			VertexColors.Add(FLinearColor(0.0f, 1.0f, 0.0f));	//Bottom left
			VertexColors.Add(FLinearColor(0.0f, 0.0f, 1.0f));	//Bottom right


		};

	};

	CustomMesh->CreateMeshSection_LinearColor(0, Vertices, Triangles, TArray<FVector>(), TArray<FVector2D>(), VertexColors, TArray<FProcMeshTangent>(), true);
}