A lot of things are bugging in my game

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!
    randomized Tile path minigame
  2. What is the issue? Include screenshots / videos if possible!
    The path isn’t turning blue and not printing there isn’t one
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried studying this one code but uh confused me so i did my own with some of their code in there

You can try this in studio


local pathwd = 10
local height = 1 
local position = height / 2

local pathstack = {}
table.insert(pathstack, { ZCell = 0, Xcell = 0 })

local row = 10
local collum = 6
local ran = Random.new()
local pathss = {}

local directions = {
	Vector3.new(1, 0, 0),   -- Right
	Vector3.new(0, 0, 1),   -- Forward
	Vector3.new(-1, 0, 0),  -- Left
	Vector3.new(0, 0, -1),  -- Backward
	Vector3.new(1, 0, 1),    -- Top-Right Diagonal
	Vector3.new(-1, 0, 1),   -- Top-Left Diagonal
	Vector3.new(1, 0, -1),   -- Bottom-Right Diagonal
	Vector3.new(-1, 0, -1)   -- Bottom-Left Diagonal
}

local function createpart(x, y, z, rx, ry, rz)
	local part = Instance.new('Part')
	part.Parent = game.Workspace
	part.Size = Vector3.new(x, y, z)
	part.Position = Vector3.new(rx, ry, rz)
	part.Anchored = true
	return part
end


for z = 0, row - 1 do
	maze[z] = {}
	for x = 0, collum - 1 do
		maze[z][x] = {  }  
	end
end 

local function tiles()
	for z = 0, row - 1 do
		for x = 0, collum - 1 do
			local tilex = x * 17
			local tilez = z * 17
			local part = createpart(pathwd, height, pathwd, tilex, position, tilez)
			if part then
				part.Name = string.format("%d,%d", z, x)
				part.Parent = game.Workspace.Folder
				maze[z][x].Tile = part  
				local valuesx = Instance.new("NumberValue",part)
				valuesx.Name = "X"
				valuesx.Value = x
				local valuesz = Instance.new("NumberValue",part)
				valuesz.Name = "Z"
				valuesz.Value = z
			end
		end
	end
end

local function getneighors(z, x)
	local neighbors = {}

	
	for _, direction in ipairs(directions) do
		if z and x then
				local newZ = z + direction.Z
		local newX = x + direction.X

	
		if newZ >= 0 and newZ < row and newX >= 0 and newX < collum then
		
			if maze[newZ][newX] and not maze[newZ][newX].Visited then
				table.insert(neighbors, maze[newZ][newX])
			end
		end
	end
		end
	

	return neighbors
end

local function reconstructpart(endn,previous)
	
	local path = {endn}
	repeat
		local cn = path[#path]

		local previousnode = previous[cn]
		if previous then
			print(previous)
			table.insert(path,previous)
		end
	until not previousnode
	return path
end

local function path(endn,startn)
	for z = 0, row - 1 do
		for x = 0, collum - 1 do
			maze[z][x].Tile.BrickColor = BrickColor.Black()
		end
	end
	repeat
		
		task.wait()
		local previous = {}
		local stackcell = pathstack[#pathstack]
		local x = stackcell.Xcell
		local z = stackcell.ZCell
		
		
		repeat
			task.wait()
			local neighbors = getneighors(z, x)
			local randomneighbor = ran:NextInteger(1, #neighbors)
			local nextcell = neighbors[randomneighbor]
			if nextcell and #neighbors > 0 then
				
				
				if nextcell then


					previous[nextcell] = stackcell

					nextcell.Visited = true
					nextcell.Tile.BrickColor = BrickColor.Red()  


					table.insert(pathstack, { ZCell = game.Workspace.Folder:FindFirstChild(nextcell.Tile.Name).Z.Value, Xcell =  game.Workspace.Folder:FindFirstChild(nextcell.Tile.Name).X.Value })
					if nextcell.Tile == endn then
					
						print(previous)
						return 	reconstructpart(endn,previous)
					end
				end
			else

				table.remove(pathstack,#pathstack)
			end
		until
		not nextcell
	
	until
	#pathstack == 0

	
end

tiles()


local startnode = game.Workspace.Folder:FindFirstChild(string.format("%d,%d", 0, math.random(0, collum - 1)))
local endnode = game.Workspace.Folder:FindFirstChild(string.format("%d,%d", row - 1, math.random(0, collum - 1)))

print("Start Node:", startnode)
print("End Node:", endnode)



local pathway = path(endnode,startnode)

if pathway then

	for i,v in pairs(pathway) do
		if v then
	
			if v ~= endnode and v ~= startnode and v.Tile then
				
			
	
				print("yes")
				v.Tile = BrickColor.Blue()
				v.Tile.CanCollide = false
			
			end
		end
	end
else
	print("no path")
end



Sorry if i reply late since am going through the hurricane.

You need to initialize the maze table by adding local maze = {} before filling it with tiles. Also, correctly set the tile’s BrickColor property by using v.Tile.BrickColor = BrickColor.Blue() instead of assigning the color to v.Tile.

I forgot to do a table for maze haha I’ll see if it works tomorrow and mark it as a solution if it works.

nothing really happened. For some reason the code up there cut off the local maze = {} part that I forgot to put.

local maze = {}

local pathwd = 10
local height = 1 
local position = height / 2

local pathstack = {}
table.insert(pathstack, { ZCell = 0, Xcell = 0 })

local row = 10
local collum = 6
local ran = Random.new()
local pathss = {}

local directions = {
	Vector3.new(1, 0, 0),   -- Right
	Vector3.new(0, 0, 1),   -- Forward
	Vector3.new(-1, 0, 0),  -- Left
	Vector3.new(0, 0, -1),  -- Backward
	Vector3.new(1, 0, 1),    -- Top-Right Diagonal
	Vector3.new(-1, 0, 1),   -- Top-Left Diagonal
	Vector3.new(1, 0, -1),   -- Bottom-Right Diagonal
	Vector3.new(-1, 0, -1)   -- Bottom-Left Diagonal
}

local function createpart(x, y, z, rx, ry, rz)
	local part = Instance.new('Part')
	part.Parent = game.Workspace
	part.Size = Vector3.new(x, y, z)
	part.Position = Vector3.new(rx, ry, rz)
	part.Anchored = true
	return part
end


for z = 0, row - 1 do
	maze[z] = {}
	for x = 0, collum - 1 do
		maze[z][x] = {  }  
	end
end 

local function tiles()
	for z = 0, row - 1 do
		for x = 0, collum - 1 do
			local tilex = x * 17
			local tilez = z * 17
			local part = createpart(pathwd, height, pathwd, tilex, position, tilez)
			if part then
				part.Name = string.format("%d,%d", z, x)
				part.Parent = game.Workspace.Folder
				maze[z][x].Tile = part  
				local valuesx = Instance.new("NumberValue",part)
				valuesx.Name = "X"
				valuesx.Value = x
				local valuesz = Instance.new("NumberValue",part)
				valuesz.Name = "Z"
				valuesz.Value = z
			end
		end
	end
end

local function getneighors(z, x)
	local neighbors = {}

	
	for _, direction in ipairs(directions) do
		if z and x then
				local newZ = z + direction.Z
		local newX = x + direction.X

	
		if newZ >= 0 and newZ < row and newX >= 0 and newX < collum then
		
			if maze[newZ][newX] and not maze[newZ][newX].Visited then
				table.insert(neighbors, maze[newZ][newX])
			end
		end
	end
		end
	

	return neighbors
end

local function reconstructpart(endn,previous)
	
	local path = {endn}
	repeat
		local cn = path[#path]

		local previousnode = previous[cn]
		if previous then
			print(previous)
			table.insert(path,previous)
		end
	until not previousnode
	return path
end

local function path(endn,startn)
	for z = 0, row - 1 do
		for x = 0, collum - 1 do
			maze[z][x].Tile.BrickColor = BrickColor.Black()
		end
	end
	repeat
		
		task.wait()
		local previous = {}
		local stackcell = pathstack[#pathstack]
		local x = stackcell.Xcell
		local z = stackcell.ZCell
		
		
		repeat
			task.wait()
			local neighbors = getneighors(z, x)
			local randomneighbor = ran:NextInteger(1, #neighbors)
			local nextcell = neighbors[randomneighbor]
			if nextcell and #neighbors > 0 then
				
				
				if nextcell then


					previous[nextcell] = stackcell

					nextcell.Visited = true
					nextcell.Tile.BrickColor = BrickColor.Red()  


					table.insert(pathstack, { ZCell = game.Workspace.Folder:FindFirstChild(nextcell.Tile.Name).Z.Value, Xcell =  game.Workspace.Folder:FindFirstChild(nextcell.Tile.Name).X.Value })
					if nextcell.Tile == endn then
					
						print(previous)
						return 	reconstructpart(endn,previous)
					end
				end
			else

				table.remove(pathstack,#pathstack)
			end
		until
		not nextcell
	
	until
	#pathstack == 0

	
end

tiles()


local startnode = game.Workspace.Folder:FindFirstChild(string.format("%d,%d", 0, math.random(0, collum - 1)))
local endnode = game.Workspace.Folder:FindFirstChild(string.format("%d,%d", row - 1, math.random(0, collum - 1)))

print("Start Node:", startnode)
print("End Node:", endnode)



local pathway = path(endnode,startnode)

if pathway then

	for i,v in pairs(pathway) do
		if v then
	
			if v ~= endnode and v ~= startnode and v.Tile then
				
			
	
				print("yes")
				v.Tile.BrickColor = BrickColor.Blue()
				v.Tile.CanCollide = false
			
			end
		end
	end
else
	print("no path")
end

Can anyone help it didn’t work

I got it working with a few changes. Fix the conditions in your path function and the reconstructpart logic. Make sure to check for previousnode properly and only insert valid paths. Also, you need to reset the visited status for each tile before starting the pathfinding and ensure that you’re correctly referencing the X and Z values. One more thing is that it’s best to make sure that you’re comparing the correct tile objects for endn and correctly changing the tile colors when the path is found.

This is what worked out for me, as far as having blue tiles:

local pathwd = 10
local height = 1
local position = height / 2

local pathstack = {}
table.insert(pathstack, { ZCell = 0, Xcell = 0 })

local row = 10
local collum = 6
local ran = Random.new()
local maze = {} -- gotta initialize maze 

local directions = {
	Vector3.new(1, 0, 0),   -- Right
	Vector3.new(0, 0, 1),   -- Forward
	Vector3.new(-1, 0, 0),  -- Left
	Vector3.new(0, 0, -1),  -- Backward
	Vector3.new(1, 0, 1),   -- Top-Right Diagonal
	Vector3.new(-1, 0, 1),  -- Top-Left Diagonal
	Vector3.new(1, 0, -1),  -- Bottom-Right Diagonal
	Vector3.new(-1, 0, -1)  -- Bottom-Left Diagonal
}

local function createpart(x, y, z, rx, ry, rz)
	local part = Instance.new('Part')
	part.Parent = game.Workspace
	part.Size = Vector3.new(x, y, z)
	part.Position = Vector3.new(rx, ry, rz)
	part.Anchored = true
	return part
end

for z = 0, row - 1 do
	maze[z] = {}
	for x = 0, collum - 1 do
		maze[z][x] = {}  
	end
end

local function tiles()
	for z = 0, row - 1 do
		for x = 0, collum - 1 do
			local tilex = x * 17
			local tilez = z * 17
			local part = createpart(pathwd, height, pathwd, tilex, position, tilez)
			if part then
				part.Name = string.format("%d,%d", z, x)
				part.Parent = game.Workspace.Folder
				maze[z][x].Tile = part
				local valuesx = Instance.new("NumberValue", part)
				valuesx.Name = "X"
				valuesx.Value = x
				local valuesz = Instance.new("NumberValue", part)
				valuesz.Name = "Z"
				valuesz.Value = z
			end
		end
	end
end

local function getneighbors(z, x)
	local neighbors = {}
	for _, direction in ipairs(directions) do
		local newZ = z + direction.Z
		local newX = x + direction.X
		if newZ >= 0 and newZ < row and newX >= 0 and newX < collum then
			if maze[newZ][newX] and not maze[newZ][newX].Visited then
				table.insert(neighbors, maze[newZ][newX])
			end
		end
	end
	return neighbors
end

local function reconstructpart(endn, previous)
	local path = {endn}
	repeat
		local cn = path[#path]
		local previousnode = previous[cn]
		if previousnode then
			table.insert(path, previousnode)
		else
			break
		end
	until cn == nil -- use start condition
	return path
end

local function path(endn, startn)
	-- reset all tiles and visited status
	for z = 0, row - 1 do
		for x = 0, collum - 1 do
			maze[z][x].Visited = false
			maze[z][x].Tile.BrickColor = BrickColor.Black()
		end
	end

	local previous = {}
	local stackcell = maze[startn.Z.Value][startn.X.Value]
	stackcell.Visited = true
	local pathstack = { stackcell }

	while #pathstack > 0 do
		local currentCell = pathstack[#pathstack]
		local z = currentCell.Tile.Z.Value
		local x = currentCell.Tile.X.Value

		local neighbors = getneighbors(z, x)
		if #neighbors > 0 then
			local randomIndex = ran:NextInteger(1, #neighbors)
			local nextcell = neighbors[randomIndex]
			previous[nextcell] = currentCell
			nextcell.Visited = true
			nextcell.Tile.BrickColor = BrickColor.Red()
			table.insert(pathstack, nextcell)

			if nextcell.Tile == endn then
				return reconstructpart(nextcell, previous)
			end
		else
			table.remove(pathstack)
		end
	end

	print("No path found")
	return nil
end

tiles()

local startnode = game.Workspace.Folder:FindFirstChild(string.format("%d,%d", 0, math.random(0, collum - 1)))
local endnode = game.Workspace.Folder:FindFirstChild(string.format("%d,%d", row - 1, math.random(0, collum - 1)))

print("Start Node:", startnode)
print("End Node:", endnode)

local pathway = path(endnode, startnode)

if pathway then
	for _, v in pairs(pathway) do
		if v then
			if v ~= endnode and v ~= startnode and v.Tile then
				print("yes")
				v.Tile.BrickColor = BrickColor.Blue()
				v.Tile.CanCollide = false
			end
		end
	end
else
	print("No path")
end

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