Need help in making a "search" for loop

I am trying to do a raycast to search for a specific object and don’t want to do it again in the same position.

Basically like this:

  • You start on a single point
    0
  • Then if nothing hits the ray, expand the search
    -1 0 1
    -1 .. 1
    -1 0 1
  • Keep expanding the search until the object is found
    -2 -1 0 1 2
    -2 . . . . . 2
    -2 . . . . . 2
    -2 -1 0 1 2

My idea was to put the for loops in a while loop
Add a local variable that counts every time the loops are finished
Put it in the start and end value of the for loop
Then stop once the “radius” gets too big

local cycle = 0
while cycle < 16 do
	cycle += 1
					
	for y = -cycle, cycle, cycle*2 do
						
		for x = -cycle, cycle, cycle*2 do			
			
		end

	end

end

But this will only search in an X pattern, not a square.
Any suggestions on how I could do that?

First, the arguments for a for loop won’t loop through all the values

--       start  stop   increment
for y = -cycle, cycle, cycle*2 do
-- y will only equal two values: -cycle and cycle, nothing inbetween

You want to loop through the sides
(Note: this will double check every corner)

for y = -cycle, cycle do
  Check(cycle, y) -- the x value of the vertical lines is constant
  Check(-cycle, y) -- but check both the left and right sides
end
-- do the same thing, but for the horizontal lines
for x = -cycle, cycle do
  Check(x, cycle)
  Check(x, -cycle)
end