Struggle with room generation

Hi , I need a bit of help with room generation .
JUNCTIONs … that’s basically the problem. Once junction start to spawn the code automatically breaks . I did some research but i cant find any solutions . I am in need of a helping hand

any help is appreciated :slight_smile:

here is the code:

function module:GetRandom(prevRoom)
	
	local totalWeight = 0
	
	for i , info in pairs(Dictionary) do
		totalWeight += info.weight
	end
	local randomweight = random:NextInteger(1 , totalWeight)
	local currentWeight = 0
	local RandomRoom
	
	for i , info in pairs(Dictionary) do
		currentWeight += info.weight
		
		if randomweight <= currentWeight then
			RandomRoom = Sections[i]
			break
		end
		
	end
		
	local direction = Dictionary[RandomRoom.Name]["Direction"]
	local HasStairs = Dictionary[RandomRoom.Name]["Stairs"]
	local prevHasStairs = Dictionary[prevRoom.Name]["Stairs"]
	local PrevRoomIsJuction = Dictionary[prevRoom.Name]["Junction"]
	
	local checkCollision = module:CheckPossibleCollision(prevRoom , PrevRoomIsJuction)
	
	if (prevRoom.Name == RandomRoom.Name)
		or (direction and direction == LastTurnDirection)
		or (HasStairs and prevHasStairs) 
		or (prevRoom:GetAttribute("Room") and RandomRoom.Name ~= "Walls#1")
		or (RandomRoom:GetAttribute("Room") and spaceUntilNextRoom ~= 0)     -- if all of thie meet then we just recycle
		
	then 
		return module:GetRandom(prevRoom)
		
	elseif (PrevRoomIsJuction) then -- checking if previous room is a junction
		
		local prevCorridor = nil -- will be used to set it at prevRoom
		
		for i , v in pairs(prevRoom.Folder:GetChildren()) do   -- remove junction connecter from Avaiableconnecter table soo it wont mix
			for indx , name in pairs(ConnectorAvaiable) do
				if v == name then
					table.remove(ConnectorAvaiable , ConnectorAvaiable[v])
					
				end
			end 
		end
		
		for i = 1 , #prevRoom:WaitForChild("Folder"):GetChildren() do  -- the prevRoom folder contains all the connecters
			local randomconector = prevRoom.Folder["Connecters" .. i]
			
			local checkCollision = module:CheckJunctionCollision(randomconector) -- checking each connecters if theres any obstacle ahead
			
			if checkCollision then

				local EndWall = Sections.EndWall:Clone()  -- basically just close the road / corridor
				EndWall.Parent = workspace.Maps.Map
				EndWall:PivotTo(randomconector:GetPivot())
			else
				
				local corridor = Sections.LongCorridor:Clone() -- create a corridor 
				corridor.Parent = workspace.Maps.Map
				corridor:PivotTo(randomconector:GetPivot())

				table.insert(ConnectorAvaiable , corridor:WaitForChild("Connecters")) -- insert the corridor connecter to the avaiableConnecteor table
				prevCorridor = corridor -- the prevCorridor will act as replcament for prevRoom 
			end

		end
		
		return module:GetRandom(prevCorridor) -- return to get random room 
		
	elseif checkCollision then -- if collssion of new room that is not a junction is true
		
		local lastConnector = nil
		
		for i , v in pairs(ConnectorAvaiable) do -- checking if the connector of the previousRoom is in the avaiableConnecter table
			if v.Parent == prevRoom then
				lastConnector = v
				table.remove(ConnectorAvaiable , ConnectorAvaiable[v]) -- remove it if found since we will not be using it anymore
				print(lastConnector)
			end
		end
		
		local EndWall = Sections.EndWall:Clone() -- create EndWall and Pivot it to the lastconnecter  aka the connecter that we removed from table just now
		EndWall.Parent = workspace.Maps.Map
		print(ConnectorAvaiable)
		EndWall:PivotTo(lastConnector:GetPivot()) -- sometimes i got error here its said nill even though its not
		--lastConnector = nil
		
	else
		
		if RandomRoom:GetAttribute("Room") then -- just some random checking
			spaceUntilNextRoom = 3
		else
			if spaceUntilNextRoom <= 0 then
				spaceUntilNextRoom = 0
			else
				spaceUntilNextRoom -= 1
			end
		end
		
		if direction then
			LastTurnDirection = direction
		end
		
		return RandomRoom
	end
	

end

its not my full script but i think this part is the problem.

2 Likes