Texture applied to Union gets stretched sometimes

I am creating pseudorandom backrooms and all of the walls union to prevent texture overlapping issues, I thought it was working at first but after a few runs the texture started getting stretched. Is this an issue with UnionOperations or my code? I’ve tried adding wait() here and there but I had no success.

Script:

--<< NotFrindow >>--

local startingPoint = Vector3.new(0, 0, 0)
local tilesGenerated = false

local tiles = game:GetService("ServerStorage").Tiles:GetChildren()

function resetStorageTilesPos()
	for i, tile in pairs(tiles) do
		tile.PrimaryPart.CFrame = CFrame.new()
	end
end

resetStorageTilesPos()

function generateTiles(startingPoint : Vector3, tileX : number, tileY : number)
	local previousTile = nil
	local walls = {}
	local floors = {}
	local roofs = {}
	for currentTileY = 1, tileY do
		for currentTileX = 1, tileX do
			local randomTile = tiles[math.random(1, #tiles)]:Clone()
			randomTile.Name = currentTileX .. " " .. currentTileY
			if currentTileX == 1 or currentTileY == 1 then
				spawn(function()
					repeat
						wait()
					until tilesGenerated
					randomTile:Destroy()
				end)
			end
			randomTile.Parent = workspace.Map.Tiles
			print(currentTileX * 20)
			randomTile.PrimaryPart.CFrame = 
				(randomTile.PrimaryPart.CFrame +
					Vector3.new(startingPoint.X + (currentTileX * 19),
						startingPoint.Y,
						startingPoint.Z + (currentTileY * 19)))
			wait()
			for i, wall in pairs(randomTile:GetChildren()) do
				if string.find(wall.Name, "Wall") then
					wall.Anchored = true
					table.insert(walls, wall)
				end
			end
			for i, floor in pairs(randomTile:GetChildren()) do
				if string.find(floor.Name, "Floor") then
					floor.Anchored = true
					table.insert(floors, floor)
				end
			end
			for i, roof in pairs(randomTile:GetChildren()) do
				if string.find(roof.Name, "Roof") then
					roof.Anchored = true
					table.insert(roofs, roof)
				end
			end
			previousTile = randomTile
		end
	end
	
	local function union(startingPart, otherParts, name)
		spawn(function()
			local startingPart = otherParts[1]
			table.remove(otherParts, 1)
			local union : UnionOperation = startingPart:UnionAsync(otherParts)
			union.Parent = workspace.Map
			union.Name = name
			union.Anchored = true

			wait()

			for i, thing in pairs(otherParts) do
				thing:Destroy()
			end
			wait(1)
			for i, texture in pairs(game:GetService("ServerStorage").Textures[name]:GetChildren()) do
				texture.Parent = union
			end
		end)
	end
	
	union(walls[1], walls, "wall")
	union(floors[1], floors, "floor")
	union(roofs[1], roofs, "roof")
	tilesGenerated = true
end

generateTiles(Vector3.new(0, 0, 0), 5, 5)

Hmm, not sure what’s happening, maybe you need to adjust the studspertile on the textures once its on the union

I’ve already tried that, decreasing them just makes the lines width decrease.

You could try this code:

--<< NotFrindow >>--

local startingPoint = Vector3.new(0, 0, 0)
local tilesGenerated = false

local tiles = game:GetService("ServerStorage").Tiles:GetChildren()

function resetStorageTilesPos()
    for i, tile in pairs(tiles) do
        tile.PrimaryPart.CFrame = CFrame.new()
    end
end

resetStorageTilesPos()

function generateTiles(startingPoint: Vector3, tileX: number, tileY: number)
    local walls = {}
    local floors = {}
    local roofs = {}
    
    for currentTileY = 1, tileY do
        for currentTileX = 1, tileX do
            local randomTile = tiles[math.random(1, #tiles)]:Clone()
            randomTile.Name = currentTileX .. " " .. currentTileY
            
            if currentTileX == 1 or currentTileY == 1 then
                spawn(function()
                    repeat
                        wait()
                    until tilesGenerated
                    randomTile:Destroy()
                end)
            end
            
            randomTile.Parent = workspace.Map.Tiles

            local tilePosition = Vector3.new(
                startingPoint.X + (currentTileX - 1) * currentTileX,
                startingPoint.Y,
                startingPoint.Z + (currentTileY - 1) * currentTileY 
            )
            
            randomTile.PrimaryPart.CFrame = randomTile.PrimaryPart.CFrame + tilePosition

            
            for i, wall in pairs(randomTile:GetChildren()) do
                if string.find(wall.Name, "Wall") then
                    wall.Anchored = true
                    table.insert(walls, wall)
                end
            end
            
            for i, floor in pairs(randomTile:GetChildren()) do
                if string.find(floor.Name, "Floor") then
                    floor.Anchored = true
                    table.insert(floors, floor)
                end
            end
            
            for i, roof in pairs(randomTile:GetChildren()) do
                if string.find(roof.Name, "Roof") then
                    roof.Anchored = true
                    table.insert(roofs, roof)
                end
            end
        end
    end
    
    local function union(startingPart, otherParts, name)
        spawn(function()
            local union : UnionOperation = startingPart:UnionAsync(otherParts)
            union.Parent = workspace.Map
            union.Name = name
            union.Anchored = true

            wait()

            for i, thing in pairs(otherParts) do
                thing:Destroy()
            end
        end)
    end

    union(walls[1], walls, "wall")
    union(floors[1], floors, "floor")
    union(roofs[1], roofs, "roof")
    tilesGenerated = true
end

generateTiles(Vector3.new(0, 0, 0), 5, 5)

What was changed?‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎

I changed the tile positions in the script.