Detecting values on nearby buttons in a grid

I am trying to make a grid of buttons, that detect the values of buttons around them (9x9 grid), doing this button by button is out of the question and no documentation seems to be useful.

Does anyone have any ideas on how I would accomplish this?

2 Likes

Is this what you are looking for? Otherwise, give us more details:

local gridSize = 9
local buttonSize = 50 -- set to what u need

local grid = {}
for i = 1, gridSize do
  grid[i] = {}
  for j = 1, gridSize do -- following lines are for actually creating the buttons
    local button = Instance.new("TextButton") n
    button.Parent = game.Workspace 
    button.Position = UDim2.new(0, (i - 1) * buttonSize, 0, (j - 1) * buttonSize) 
    button.Size = UDim2.new(0, buttonSize, 0, buttonSize) 
    grid[i][j] = button 
  end
end

--initialize button values, should be 81 lines here
grid[1][1].Text = "5"
grid[1][2].Text = "2"
grid[1][3].Text = "7"
-- and so forth...

local function getNeighbors(x, y)
  local neighbors = {}
  for i = -1, 1 do
    for j = -1, 1 do
      if i ~= 0 or j ~= 0 then 
        local nx, ny = x + i, y + j
        if nx >= 1 and nx <= gridSize and ny >= 1 and ny <= gridSize then
          table.insert(neighbors, grid[nx][ny])
        end
      end
    end
  end
  return neighbors
end

local x, y = 5, 5 -- Example coordinate to get
local neighbors = getNeighbors(x, y)
for _, neighbor in pairs(neighbors) do
  print(neighbor.Text)
end
local part = script.Parent
local db = true
local speed = 1
local numlab = script.Parent.SurfaceGui.TextLabel
local aliveneighbors = 0



--while db == true do 



part.hitbox.Alive:GetPropertyChangedSignal("Value"):Connect(function()

	task.wait(speed)
	
	local touichingparts = part.hitbox:GetTouchingParts()

	
	for neighborsnum, parts in pairs(touichingparts) do 
		
		--
		
		
		end
			
	--end



	




	numlab.Text = aliveneighbors	

	if aliveneighbors > 4 then
		part.Color = Color3.new(0, 0, 0)
		part.hitbox.Alive.Value = false
	
	
	
	
		
	end
	

end)



local value = part.hitbox.Alive.Value



part.ClickDetector.MouseClick:Connect(function()
	if value == true then
		value = false
	else
		value = true
	end


	
	
	
end)

Is what I have so far, cant figure out how to keep track of the number of tiles around that are true/false though