Random Maze Generator by me

Hey so I’ve been working on this game that is a remake of the first ever horror game and I made a random maze generator all by myself, the only problem is that sometimes it might make something impossible and the server has to close and make another server to get a different maze. Here’s a showcase:


Please let me know what you think and how I could fix the impossible mazes
This is the first time I’ve ever made a system this complex.
Script:

local ss = game.ServerStorage
wait(1)
local children = workspace.Templatecells1:GetChildren()
for i = 1, #children do
   for i, child in ipairs(children) do
   	if child.Name == "CELLER" and not child.Used.Value then
   		wait(0.02)
   		local newroom = {ss.Rooms.Room1,ss.Rooms.Room2,ss.Rooms.Room3,ss.Rooms.Room4,ss.Rooms.Room5,ss.Rooms.Room6,ss.Rooms.Room7,
   			ss.Rooms.Room8,ss.Rooms.Room9,ss.Rooms.Room10,ss.Rooms.Room11,ss.Rooms.Room12,ss.Rooms.Room13,ss.Rooms.Room14,
   		ss.Rooms.Room15,ss.Rooms.Room16,ss.Rooms.Room17}
   		local anewroo = newroom[math.random(#newroom)]:Clone()
   		anewroo.Parent = workspace.Maze1
   		anewroo:MoveTo(child.Position)
   		print("piece number " .. numba .. ": " .. anewroo.name)
   		numba = numba + 1
   		child.Used.Value = true
   	elseif child.Name == "CELLEST" and not child.Used.Value then
   		local newroom = {ss.Boards.Room1,ss.Boards.Room2,ss.Boards.Room3,ss.Boards.Room4}
   		local anewroo = newroom[math.random(#newroom)]:Clone()
   		anewroo.Parent = workspace.Maze1
   		anewroo:MoveTo(child.Position)
   		print("piece number " .. numba .. ": " .. anewroo.name)
   		child.Used.Value = true
   	elseif child.Name == "MONSTER" and not child.Used.Value then
   		local newroom = {ss.Boards.monsterroom}
   		local anewroo = newroom[math.random(#newroom)]:Clone()
   		anewroo.Parent = workspace.Maze1
   		anewroo:MoveTo(child.Position)
   		print("piece number " .. numba .. ": " .. anewroo.name)
   		child.Used.Value = true
   	elseif child.Name == "SPAWN" and not child.Used.Value then
   		local newroom = {ss.Boards.spawnroom}
   		local anewroo = newroom[math.random(#newroom)]:Clone()
   		anewroo.Parent = workspace.Maze1
   		anewroo:MoveTo(child.Position)
   		print("piece number " .. numba .. ": " .. anewroo.name)
   		child.Used.Value = true
   		elseif child.Used.Value then
   			child:remove()
   		end
   		end
   	end
3 Likes

try to make it so that if there is a impossible part, then the maze regen’s itself

have pathfinding service generate a path, if its impossible, regenerate the maze

if it has parts, like doors, you could customize the parameters in the pathfinding to exclude those