How can I adjust this without overcomplicating/breaking it?

The point of this code is to create a sort of random land generation system over time

function findLandPosition()
	
	
	--choosing a random land piece to try 
	local landPieces = game.Workspace.landPieces.validLandPieces:GetChildren()
	local n = math.random(1, #landPieces)
	local landPiece = landPieces[n]
	
	
	--setting up OverLapParams
	local olParams = OverlapParams.new()
	olParams.FilterDescendantsInstances = {game.Workspace.Baseplate}
	olParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	
	--checking for free spaces adjacent to land piece
	local spaceCheck = game.Workspace:GetPartBoundsInRadius(landPiece.Position + Vector3.new(0,0,64), .1, olParams)

	if #spaceCheck == 0 then --if nothing is there, create the land mass
		createLand(landPiece.Position + Vector3.new(0,0,64))
	else
		--otherwise, check again in a new location
		local spaceCheck2 = game.Workspace:GetPartBoundsInRadius(landPiece.Position + Vector3.new(0,0,-64), .1, olParams)
		if #spaceCheck2 == 0 then 
			createLand(landPiece.Position + Vector3.new(0,0,-64))
		else
			
			local spaceCheck3 = game.Workspace:GetPartBoundsInRadius(landPiece.Position + Vector3.new(64,0,0), .1, olParams)
			if #spaceCheck3 == 0 then 
				createLand(landPiece.Position + Vector3.new(64,0,0))
			else
				
				local spaceCheck4 = game.Workspace:GetPartBoundsInRadius(landPiece.Position + Vector3.new(-64,0,0), .1, olParams)
				if #spaceCheck4 == 0 then 
					createLand(landPiece.Position + Vector3.new(-64,0,0))
				else
					landPiece.Parent = game.Workspace.landPieces
					print("Land cannot be made")
					findLandPosition()
				end
				
			end
			
		end
		
	end

so this all works, its not broken, its just that it has a tendency to build in a certain direction from spawn, which of course makes sense because of the way im doing it, since it always checks in the same order
and thats the problem
its going
β€œis there anything here? yes ok how about here?” and it always checks each direction in that order
so

how could i adjust this script so that it picks a random direction to try and expand in, without making the script overcomplicated, messy, or hard to read?

heres one of the little blocks since its basically copy and pasted 4 times

local spaceCheck = game.Workspace:GetPartBoundsInRadius(landPiece.Position + Vector3.new(0,0,64), .1, olParams)

	if #spaceCheck == 0 then --if nothing is there, create the land mass
		createLand(landPiece.Position + Vector3.new(0,0,64))
	else
		--otherwise, check again in a new location
		local spaceCheck2 = game.Workspace:GetPartBoundsInRadius(landPiece.Position + Vector3.new(0,0,-64), .1, olParams)
		if #spaceCheck2 == 0 then 
			createLand(landPiece.Position + Vector3.new(0,0,-64))
		else
1 Like

Something like

local SPACE_CHECK_DIRECTIONS = {
    Vector3.FromNormalId(Enum.NormalId.Front),
    Vector3.FromNormalId(Enum.NormalId.Back)
    Vector3.FromNormalId(Enum.NormalId.Right),
    Vector3.FromNormalId(Enum.NormalId.Left)
}
local LAND_SIZE = 64

local function shuffled(t)
    --From https://stackoverflow.com/questions/35572435/how-do-you-do-the-fisher-yates-shuffle-in-lua
    -- didn't check if it works
    local s = {}
    for i = 1, #t do s[i] = t[i] end
    for i = #t, 2, -1 do
        local j = math.random(i)
        s[i], s[j] = s[j], s[i]
    end
    return s
end


for _, direction in pairs(shuffled(SPACE_CHECK_DIRECTIONS)) do
    local newLandPos = landPiece.Position + direction * LAND_SIZE
    if #spaceCheck(newLandPos) > 0 then continue end
    
    createLand(landPiece.Position + direction * LAND_SIZE)
end