Parts of maze not getting connected to the main maze

  1. What do you want to achieve?
    I want to make a randomly generated maze but with this(It’s for a horror game rn its kinda ugly)
    unit
    Structure:
    anotherunit

  2. What is the issue?
    Some parts of the maze dont connect to the main one
    proof:

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried lowering the amount of parts that could be destroyed in the loop, but it still didnt work
    here’s the main code(im using oop for the first time im still a noob xd)
    ModuleScript:

local rep = game.ReplicatedStorage
local unit = rep:WaitForChild("Unit")
local mapFolder = game.Workspace:WaitForChild("MAP")
local specialRooms = workspace:WaitForChild("SPECIAL_ROOMS")
local Map = {}
Map.__index = Map
function Map.new(SizeX,SizeY,Difficulty)
	local newMap = {}
	setmetatable(newMap,Map)
	newMap.SizeX = SizeX
	newMap.SizeY = SizeY
	newMap.Difficulty = Difficulty
	
	return newMap
end
function Map:CreateMapUnit(X,Y,where)
	local x = X / 118
	local y = Y / 118
	local clone = unit:Clone()
	clone.Name =tostring(x)
	clone.Parent = where
	clone:PivotTo(CFrame.new(Vector3.new(X,20,Y)))
	spawn(function()
		wait(0.1)
		if y == 1 then
			print("up")
			clone.UP:Destroy()
		end
		if y == self.SizeY then
			print("down")
			clone.DOWN:Destroy()
		end
		if x == 1 then
			print("left")
			clone.LEFT:Destroy()
		end
		if x == self.SizeX then
			print("right")
			clone.RIGHT:Destroy()
		end
	end)
	
end
function Map:DestroyStartHoritzontal()

	for y = 1,self.SizeY,1 do
		local AmountLeft = (self.SizeY * 0.5)

		for x = 1,self.SizeX,1 do
			print("start")
			local shouldRemove = math.random(1,4)
			if shouldRemove == 1 and AmountLeft > 0 then
				AmountLeft = AmountLeft - 1
				if mapFolder[x][y]:FindFirstChild("RIGHT") then
					local rightCFrame = mapFolder[x][y].RIGHT:GetPivot()
					mapFolder[x][y].RIGHT:Destroy()
					mapFolder[x][y].CENTER.BLOCK_RIGHT.Transparency = 0
					mapFolder[x][y].CENTER.BLOCK_RIGHT.CanCollide = true
					if mapFolder[x]:FindFirstChild(y + 1) then
						mapFolder[x][y+1].CENTER.BLOCK_LEFT.Transparency = 0	
						mapFolder[x][y+1].CENTER.BLOCK_LEFT.CanCollide = true
					end
				
					
					shouldRemove = math.random(1,2)
					if shouldRemove == 2  and mapFolder[x]:FindFirstChild(y + 1) then
						local roomChosen = specialRooms:GetChildren()[math.random(1,#specialRooms:GetChildren())]:Clone()
						roomChosen.Parent = mapFolder[x][y+1]
						local offset = CFrame.new(0,0,0)
						if roomChosen.Name == "TIER1_SHOP" then
							offset =  CFrame.new(25,3,0)
						end
						roomChosen:PivotTo(mapFolder[x][y+1].LEFT:GetPivot()  * offset)
						
						mapFolder[x][y+1].LEFT:Destroy()
						AmountLeft = AmountLeft - 1
						
						mapFolder[x][y+1].CENTER.BLOCK_LEFT.Transparency = 1
						mapFolder[x][y+1].CENTER.BLOCK_LEFT.CanCollide = false
						mapFolder[x][y].CENTER.BLOCK_RIGHT.Transparency = 1
						mapFolder[x][y].CENTER.BLOCK_RIGHT.CanCollide = false
						shouldRemove = 500
					end
					if mapFolder[x]:FindFirstChild(y+1) and shouldRemove ~= 500  then
						if mapFolder[x][y+1]:FindFirstChild("LEFT") then
							mapFolder[x][y+1].LEFT:Destroy()
							AmountLeft = AmountLeft - 1

						end

					end
				end
				
			end
			if shouldRemove == 2 and  mapFolder[x]:FindFirstChild(y + 1) then
				if mapFolder[x][y+1]:FindFirstChild("LEFT") then
					local lefttCFrame = mapFolder[x][y].RIGHT:GetPivot()

					mapFolder[x][y+1].LEFT:Destroy()
					AmountLeft = AmountLeft - 1

					
					if mapFolder[x]:FindFirstChild(y) then
						mapFolder[x][y].CENTER.BLOCK_RIGHT.Transparency = 0	
						mapFolder[x][y].CENTER.BLOCK_RIGHT.CanCollide = true
					end

					mapFolder[x][y+1].CENTER.BLOCK_LEFT.Transparency = 0	
					mapFolder[x][y+1].CENTER.BLOCK_LEFT.CanCollide = true
					
					shouldRemove = math.random(1,2)
					if shouldRemove == 2  and mapFolder[x]:FindFirstChild(y) then
						mapFolder[x][y].RIGHT:Destroy()
						AmountLeft = AmountLeft - 1

						mapFolder[x][y+1].CENTER.BLOCK_LEFT.Transparency = 1
						mapFolder[x][y+1].CENTER.BLOCK_LEFT.CanCollide = false
						mapFolder[x][y].CENTER.BLOCK_RIGHT.Transparency = 1
						mapFolder[x][y].CENTER.BLOCK_RIGHT.CanCollide = false
						
						shouldRemove = 500

					end
					if mapFolder[x]:FindFirstChild(y) and shouldRemove ~= 500  then
						if mapFolder[x][y]:FindFirstChild("RIGHT") then
							mapFolder[x][y].RIGHT:Destroy()
							AmountLeft = AmountLeft - 1

							mapFolder[x][y].CENTER.BLOCK_RIGHT.Transparency = 0	
							mapFolder[x][y].CENTER.BLOCK_RIGHT.CanCollide = true

						end

					end
				end 
				
			end 
		end
	end 
end
function Map:DestroyStartVertical()

	for y = 1,self.SizeX,1 do
		local AmountLeft = (self.SizeX * 0.5)


		for x = 1,self.SizeY,1 do
			

			local shouldRemove = 0
			if not mapFolder[y][x]:FindFirstChild("RIGHT") or not not mapFolder[y][x]:FindFirstChild("LEFT") then
				print("more likely")
				shouldRemove = math.random(1,3)
			else
				shouldRemove = math.random(1,7)

			end

			if shouldRemove == 1 and AmountLeft > 0 then
				if mapFolder[y][x]:FindFirstChild("UP") then
					mapFolder[y][x].UP:Destroy()
					AmountLeft = AmountLeft - 1

					if y ~= 1 then
						mapFolder[y-1][x].CENTER.BLOCK_DOWN.Transparency = 0
						mapFolder[y-1][x].CENTER.BLOCK_DOWN.CanCollide = true
					end
				
					mapFolder[y][x].CENTER.BLOCK_UP.Transparency = 0
					mapFolder[y][x].CENTER.BLOCK_UP.CanCollide = true

					shouldRemove = math.random(1,2)
					if shouldRemove == 2  and y ~= 1 then
						mapFolder[y-1][x].DOWN:Destroy()
						AmountLeft = AmountLeft - 1


						mapFolder[y][x].CENTER.BLOCK_UP.Transparency = 1
						mapFolder[y][x].CENTER.BLOCK_UP.CanCollide = false
						mapFolder[y-1][x].CENTER.BLOCK_DOWN.Transparency = 1
						mapFolder[y-1][x].CENTER.BLOCK_DOWN.CanCollide = false
						shouldRemove = 500
					end
					if y ~= 1 and shouldRemove ~= 500  then
						if mapFolder[y-1][x]:FindFirstChild("DOWN") then
							mapFolder[y-1][x].DOWN:Destroy()
							AmountLeft = AmountLeft - 1


						end
					end
				end

			end
			if shouldRemove == 2 and y ~= 1 then
				
				if mapFolder[y-1][x]:FindFirstChild("DOWN") then
					mapFolder[y-1][x].DOWN:Destroy()
					AmountLeft = AmountLeft - 1

					mapFolder[y-1][x].CENTER.BLOCK_DOWN.Transparency = 0
					mapFolder[y-1][x].CENTER.BLOCK_DOWN.CanCollide = true
					if mapFolder[y]:FindFirstChild(x) then
						mapFolder[y][x].CENTER.BLOCK_UP.Transparency = 0
						mapFolder[y][x].CENTER.BLOCK_UP.CanCollide = true
					end
				
					shouldRemove = math.random(1,2)
					if shouldRemove == 2  and y ~= 1 then
						mapFolder[y][x].UP:Destroy()
						AmountLeft = AmountLeft - 1

						mapFolder[y][x].CENTER.BLOCK_UP.Transparency = 1
						mapFolder[y][x].CENTER.BLOCK_UP.CanCollide = false
						mapFolder[y-1][x].CENTER.BLOCK_DOWN.Transparency = 1
						mapFolder[y-1][x].CENTER.BLOCK_DOWN.CanCollide = false
						shouldRemove = 500

					end
					if y ~= 1 and shouldRemove ~= 500  then
						if mapFolder[y][x]:FindFirstChild("UP") then
							mapFolder[y][x].UP:Destroy()
							AmountLeft = AmountLeft - 1

						end

					end
				end 
			end 
		
		end
		
	end 
	
end

function Map:StartBuildingMap()
	for y = 1,self.SizeY,1 do
		local folder = Instance.new("Folder",mapFolder)
		folder.Name = y
		
		for x = 1,self.SizeX,1 do
			
			
			self:CreateMapUnit(x * 118,y * 118,folder)
			
		end
	end
end

return Map

MainScript:

local rep = game.ReplicatedStorage
local modules = rep:WaitForChild("Modules")
local map = require(modules.Map)

local newMap = map.new(5,5,0)

newMap:StartBuildingMap()
wait(2)
print("Forming path")

newMap:DestroyStartHoritzontal()
newMap:DestroyStartVertical()

English isn’t my first language so sorry if i talk like a 5 year old kid

3 Likes

I forgot to mention that because the players will spawn randomly in the maze, every cell(or whatever its called) has to be connected by at least 1 part

im cooked :frowning:

Correct me if I’m wrong, but just skimming over your code, I don’t see anything that looks like an actual maze generation algorithm, at least not one I’m familiar with. It looks like just some scanning passes that are removing connections at random and at map edges. If this is true, and it’s just random removals, then complete connectivity is certainly not guaranteed. You will need to implement an actual maze generation algorithm that has this property. The simplest, and the one most commonly used for winding and branching mazes in games is the recursive backtracking algorithm. But there are others, like Prims, which may be more suitable if in some scenerios, like if you want short deadends to make an office cubicle type maze. Different algorithms make mazes with different characteristics.

Once you have a fully and simply-connected maze, you can always add more connections back in, to create loops or open up areas like courtyards, but you can’t remove paths arbitrarily without possibly disconnecting sections.

yes you’re right, i didn’t feel like using the algortithms bc i thought i could make one by myself haha, i’ll check the backtracking one that you recommended me, thanks :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.