Infinite Hallway Problem with Room Generation

I am making an Infinite Hallway game with my friend and I came across a problem. The infinite hallway part works perfectly but it is the room generation that I am having trouble with.

Script:

local repStorage = game:GetService("ReplicatedStorage")
local NumberRooms = 0
local db = true
local rooms = repStorage.Rooms

for _,v in pairs(rooms:GetChildren()) do
	NumberRooms += 1
end

local random1 = math.random(1, NumberRooms)
local random2 = math.random(1, NumberRooms)

local room1 = 0
local room2 = 0

script.Parent.Touched:Connect(function(hit)
	if db == true then
		db = false
		if hit.Parent:FindFirstChild("Humanoid") then
			local newHallway = script.Parent.Parent:Clone()
			newHallway:SetPrimaryPartCFrame(CFrame.new(newHallway.PrimaryPart.Position + Vector3.new(149, 0, 0)))
			newHallway.Parent = game.Workspace
			script.Parent.Parent.EndShadow:Destroy()
			
			local room1 = rooms[tostring(random1)]:Clone()
			local room2 = rooms[tostring(random2)]:Clone()
			
			task.wait(1)
			
			room1:SetPrimaryPartCFrame(CFrame.new(newHallway.Room1.Position))
			room2:SetPrimaryPartCFrame(CFrame.new(newHallway.Room2.Position))
			
			room1.Parent = workspace
			room2.Parent = workspace
			print("Done")
			script:Destroy()
		end
	end
end)

The script is in the floor of the hallway.

The rooms all spawn where the Room1 attachment’s original position is. I don’t really know how to attach the rooms to the new position that the attachments have. If anyone knows or has an idea on the solution please reply below!

Thank you!

If room1 and room2 are Attachments dont use position but worldposition.

Sorry, they are parts and I call them attachments because they are where the rooms attach. I think that is a problem because WorldPosition is not a valid member of a part.

Is there any way I could do it with parts or should I change them to actual attachments?

no no, parts are the way to go, was just confused when you were calling them Attachments.

But now that I know they are parts, I had this issue before with my Cart ride track system. This is what I did with them if this helps.

I set a variable called last. So in your case local last = nil. It is set to nil since there is currently no last room. You would then check if last is == nil if so then you create a room at the starting point which then setting last = room.

Here is what it would look like:

local rooms = game:GetService("ReplicatedStorage").Rooms
local lastRoom = nil --// Sets to nil since there is no current room.

for i = 1, 100 do --// This is for testing. You can set this up to your touched event.
	if (lastRoom==nil) then --// If the there isnt a current room make a starting point.
		local randomRoom = rooms:GetChildren()[math.random(1, #rooms:GetChildren())]:Clone() --// Gets a random room
		randomRoom:SetPrimaryPartCFrame(CFrame.new(0,0,0)) --// The starting CFrame
		randomRoom.Parent = game:GetService("Workspace")
		lastRoom = randomRoom --// Finally setting the start room!
	else --// There is a room to go off of.
		local randomRoom = rooms:GetChildren()[math.random(1, #rooms:GetChildren())]:Clone()
		randomRoom:SetPrimaryPartCFrame(lastRoom.End.CFrame) --// Set it to the part you want it to go to.
		randomRoom.Parent = game:GetService("Workspace")
		lastRoom = randomRoom --// Thast setting the lastRoom to that room.
	end
end