Problem with making a "dungeon" generator

I try to make a dungeon generator

function GenerateRoom(position,rotation)
	if roomsNumber >= roomsNumberMax then return end
	local whichRoom = RoomsNames[math.random(1,#RoomsNames)]
		local room = whichRoom:Clone()
		room.Parent = game.Workspace.Facility
		local attachment = room.PrimaryPart:FindFirstChildWhichIsA("Attachment")
		if attachment then
			room:PivotTo(CFrame.new(position + attachment.Position))
			local CFRot1 = CFrame.lookAt(room.PrimaryPart.Position,attachment.WorldPosition)
			local CFRot = CFrame.lookAt(attachment.WorldPosition,position)
			room:PivotTo(CFrame.new(position + attachment.Position)*CFrame.fromEulerAnglesXYZ(CFRot:ToEulerAnglesXYZ()))
			attachment:Destroy()
		end

I know that this is messy but i tried a lot of things and this how i ended up. It doesnt work rotation is bad and position

can you send a video of it malfunctioning? Im having a similar issue.

edit: Oh sheesh i realized this reply is a year late, thought was 2 weeks

I mean this works, its old as you realized and it looks pretty bad. Spaghetti

local temp = Instance.new("Part")
		local previousPrimary = room.PrimaryPart
		temp.Anchored = true
		temp.Parent = room
		room.PrimaryPart = temp
		
		temp.CFrame = attachment.WorldCFrame*CFrame.fromEulerAnglesXYZ(0,math.rad(180),0)
		--temp.CFrame = CFrame.lookAt(temp.Position,Vector3.new(room.PrimaryPart.Position.X,temp.Position.Y,room.PrimaryPart.Position.Z))
		room:PivotTo(pos)
		local door = ServerStorage.Facility.MainRoomsExtras.Door:Clone()
		local temp2 = Instance.new("Part")
		temp2.Anchored = true
		temp2.Parent = door
		door.Parent = game.Workspace.Facility
		door.PrimaryPart = temp2
		
		temp2.CFrame = door.Primary.Attachment.WorldCFrame*CFrame.fromEulerAnglesXYZ(0,math.rad(180),0)
		door:PivotTo(pos)

but it works, you can use it if you want ig