Random Maze Generator only working one time

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am trying to make a maze that keeps regenerating every time a intermission is done
so every time it regenerates it keeps a random pattern

  1. What is the issue? Include screenshots / videos if possible!

The maze is generating only 1 time per server created,

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried to wrap it around a module script with no succes, then later on i have tried to make a function at the end to recall all the others, but again. it only created the walls and not acttualy played any find pattern

Code down bellow

local maze = {}
local pathwayWidth = 10 --the path width
local wallHeight = 10 --the walls height
local wallthic = 1 --the walls thickness
local cols = 8  -- self explaing the ammount of colns in a maze
local rows = 8  --self explaing the ammount of rows in the maze
local mazeoffsetX = 0  --these are for moving the maze up and down
local mazeoffsetZ = 0  --these are for moving the maze left and right

--The things above are customizble and you can change the propiteies how you want it
--Hope its what you wanted - x_Kranxy <3

local walllen = pathwayWidth+2*wallthic
local wallHitPos = wallHeight/2
local wallOffset = pathwayWidth/2+wallthic/2
local floorHight = 2
local floorhitPos = floorHight/2
local floorTileDistance = pathwayWidth + wallthic

local stack = {}
table.insert(stack, {zVal=0, xVal=0})
local rng = Random.new()
local colorRng = math.random(1,5)
local color = nil

if colorRng == 1 then
	color = "White"
elseif colorRng == 2 then
	color = "Sea green"
elseif colorRng == 3 then
	color = "Silver"
elseif colorRng == 4 then
	color = "Pastel Blue"
elseif colorRng == 5 then
	color = "Wheat"


end

local function createPart(x,y,z,px,py,pz)
	local part = Instance.new("Part",workspace)
	part.Anchored = true
	part.BrickColor = BrickColor.new(color)
	part.Size = Vector3.new(x,y,z)
	part.Position = Vector3.new(px, py, pz) + Vector3.new(mazeoffsetX,0,mazeoffsetZ)
	part.TopSurface = Enum.SurfaceType.Smooth
	return part
end

local function createFloor()
	for z=0, rows-1,1 do
		maze[z]= {}
		for x=0, cols-1,1 do
			local posx = x*floorTileDistance
			local pozz = z*floorTileDistance
			local part = createPart(pathwayWidth+wallthic,floorHight,pathwayWidth+wallthic,posx,floorhitPos,pozz)
			maze[z][x] = {tile = part}
		end
	end
end

local function createWalls()
	for z=0, rows-1,1 do
		for x=0, cols-1,1 do
			--EastWalls
			local posx = x*floorTileDistance+wallOffset
			local pozz = z*floorTileDistance
			local part = createPart(wallthic,wallHeight,walllen,posx,wallHitPos,pozz)
			maze[z][x].eastWall = part
			if maze[z][x+1] then
				maze[z][x+1].westWall = part
			end
			--SouthWalls
			local posx = x*floorTileDistance
			local pozz = z*floorTileDistance+wallOffset
			local part = createPart(walllen,wallHeight,wallthic,posx,wallHitPos,pozz)
			maze[z][x].sotuhWall = part
			if maze[z+1] then
				maze[z+1][x].northWall = part
			end
			--edge walls
			if x== 0 then
				local posx = -wallOffset
				local pozz = z*floorTileDistance
				 createPart(wallthic,wallHeight,walllen,posx,wallHitPos,pozz)
			end
			
			if z==0 and x~=0 then
				local posx = x*floorTileDistance
				local pozz = -wallOffset
				 createPart(walllen,wallHeight,wallthic,posx,wallHitPos,pozz)
			end
		end
	end
end

local function removeWalls(wall)
	local s = wall.Size
	local p = wall.Position
	wall.Size = Vector3.new(s.X-wallHeight-1,s.Z)
	wall.Position = Vector3.new(p.Z,floorHight,s.Z)
	wall.BrickColor = BrickColor.White()
end

local function redrawMaze()
	for z=0, rows-1,1 do
		for x=0, cols-1,1 do
			local cell = maze[z][x]
			if cell.visited then
		    cell.tile.BrickColor = BrickColor.White() 
				if cell.northPath then
					removeWalls(cell.northWall)
				end
				if cell.southPath then
					removeWalls(cell.sotuhWall)
				end
				if cell.eastPath then
					removeWalls(cell.eastWall)
				end
				if cell.westPath then
					removeWalls(cell.westWall)
				end
			end
		end
	end
end

local function getNeighbours(z,x)
	local neigbours = {}
	if maze[z-1] and not maze[z-1][x].visited then
		table.insert(neigbours,0)
	end
	if maze[z][x+1] and not maze[z][x+1].visited then
		table.insert(neigbours,1)
	end
	if maze[z+1] and not maze[z+1][x].visited then
		table.insert(neigbours,2)
	end
	if maze[z][x-1] and not maze[z][x-1].visited then
		table.insert(neigbours,3)
	end
	return neigbours
end

local function searchPath()
	if stack==nil or #stack==0  then
		return false
	end
	local stackCell = stack[#stack]
	local x = stackCell.xVal
	local z = stackCell.zVal
	maze[z][x].tile.BrickColor = BrickColor.Green()
	wait()
	local neigbours = getNeighbours(z,x)
	if #neigbours >0 then
		local idx = rng:NextInteger(1,#neigbours)
		local nextCellDir = neigbours[idx]
		if nextCellDir == 0 then
			maze[z][x].northPath = true
			maze[z-1][x].southPath = true
			maze[z-1][x].visited = true
			table.insert(stack, {zVal=z-1,xVal=x})
		elseif nextCellDir == 1 then
				maze[z][x].eastPath = true
				maze[z][x+1].westPath = true
				maze[z][x+1].visited = true
			table.insert(stack, {zVal=z,xVal=x+1})
		elseif nextCellDir == 2 then
				maze[z][x].southPath = true
				maze[z+1][x].northPath = true
				maze[z+1][x].visited = true
			table.insert(stack, {zVal=z+1,xVal=x})
		elseif nextCellDir == 3 then
				maze[z][x].westPath = true
				maze[z][x-1].eastPath = true
				maze[z][x-1].visited = true
				table.insert(stack, {zVal=z,xVal=x-1})
		end
	else
		table.remove(stack, #stack)
	end
	return true
end

createFloor()
createWalls()
maze[0][0].visited = true
wait(5)
while searchPath() do
	redrawMaze()
	wait()
end
maze[rows-1][cols-1].sotuhWall.Transparency = 1
maze[rows-1][cols-1].sotuhWall.CanCollide = false
print("Finnished")
2 Likes

what would an intermission be in tbe script? when the maze generation is finished?

1 Like

A normal countdown with a text label, after its done it sends a remoteEvent to the server to destroy the maze from workspace

Put all the code that generates a maze into a function, then put that in a ModuleScript that returns the function.

require it from a different script that manages rounds- related logic and call the function every round. Probably make the function also clean up any existing maze, or write a different function for that.

2 Likes

Thank you, i didnt really thought about that. I did the same for the most part. One bug was that the maze wouldnt solve itself. But now i got it fix. Thank you!

1 Like

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