My room generation has multiple bugs and I am stuck trying to fix them

So recently I have been working on this maze generation thing that I made a couple of months ago and stopped working on. But there is a problem with it. What I want, is for it to spawns in a room for all the nodes available, check if it’s hitbox is colliding with another hitbox, and if it is, it deletes the room, and turns the endnode the room was placed on opaque. But if it wasn’t colliding, it would delete the endnode it was attached to and make more rooms off of that room.

The issue, is that sometimes there are floating chunks of rooms clustered together, there are rooms spawning inside of rooms which this video demonstrates:


As you can see, the split path one is spawning inside of it. It doesn’t throw errors because it was one of the last rooms to spawn I think which makes it so that It doesn’t get affected by some changes. The collisions work perfectly fine, I think the only issue is that I need to reorder the code and change some parts of it to make it more efficient but I don’t know yet what to do. Here is the script that I have.

local Rooms = game.ReplicatedStorage.Clones
local level = Rooms.Beginning:Clone()
local limit = 100
local EndNodes = nil
level.Parent = workspace.Rooms
level:PivotTo(CFrame.new(0,12.1,0))
rooms = 0
touchingparts = {}

function createRooms()
	EndNodes = game.Workspace.Rooms:GetChildren()
	for i, Room in ipairs(EndNodes) do
		local Number = math.round(math.random(1, 3)) 
		for _, endnodes in ipairs(Room:GetChildren()) do
			print(endnodes)
			if endnodes.Name == "EndNode" and rooms < limit then
				level = Rooms[tostring(Number)]:Clone()
				level.Parent = workspace.Rooms
				if rooms == 0 then
					level:PivotTo(workspace.Rooms.Beginning.EndNode.CFrame)
				else
					level:PivotTo(endnodes.CFrame)
				end
				if Room ~= game.Workspace.Rooms.Beginning and Room.Hitbox then
					touchingparts = Room.Hitbox:GetTouchingParts()
				else
					touchingparts = {}
				end
				if #touchingparts > 0 and Room ~= workspace.Rooms.Beginning then
					Room:Destroy()
					level:Destroy()
					break
				else
					if Room.EndNode then
						endnodes:Destroy()
					end
				end
				print("Touching: "..#touchingparts)
				rooms = rooms + 1
				oldlevel = level
			elseif Room:FindFirstChild("EndNode") and rooms == limit then
				for _, Room in ipairs(EndNodes) do
					if Room ~= game.Workspace.Rooms.Beginning and Room.Hitbox then
						touchingparts = Room.Hitbox:GetTouchingParts()
					else
						touchingparts = {}
					end
					if #touchingparts > 0 and Room ~= workspace.Rooms.Beginning then
						Room:Destroy()
						level:Destroy()
						Room.EndNode.Transparency = 0
						Room.EndNode.CanCollide = true
						Room.EndNode.Name = "DeadEnd"
					end
				end
			end
		end
	end	
end

while rooms < limit do
	createRooms()
	task.wait(1)
	print(rooms)
end


If anymore information is needed, just ask me for it, and thanks for helping out. Also the hitbox detection is a little buggy.