Creating a table of all possible positions in a baseplate by each stud

As the title says I’m trying to create a table that will store all the possible positions in a baseplate by each stud. Mainly for a random spawning script. (I want to take a brick and use code to find each position possible) For example;

-- 2x2 baseplate

local positions = {
0.77, 6.88, 4.04,
-0.23, 6.88, 4.04,
-0.23, 6.88, 5.04,
0.77, 6.88, 5.04,
}

I’m not too sure how I can do this so if anyone can put me in the right direction that’d be great.
Thanks for reading.

1 Like

You might want to check this:

2 Likes

This would just be a simple 2D array (unless your map has depth in which case it could be 3D), but in order to get all the positions you would want to know the bounds of your baseplate’s size. What is the X? What is the Z? From there you can start from the corner of the baseplate, likely the position (0,0,0), or the top left corner of the baseplate. From there you can just iterate size.X / increment and size.Z / increment times creating a 2D array of all the positions.

It would probably look something like this:

local startPosition = baseplate.Position - Vector3.new(baseplate.Size.X / 2, 0, baseplate.Size.Z / 2)

for i = 1,(baseplate.Size.X / increment) do
	for j = 1,(baseplate.Size.Z / increment) do
		table.insert(positions, startPosition + Vector3.new(i, 0, j))
	end
end
5 Likes

Now he just needs functions to convert between the indices and positions.

local function PosToId(p)
   return (p.Y-1)*GridSize.X+p.X
end

local function IdToPos(id)
   local x = (id-1)%GridSize.X+1
   local y = (id-x)/GridSize.X+1
   return Vector2.new(x,y)
end
2 Likes

This seems to get way too spawns.

(printed the amount in the table)

My baseplate size is 512, 20, 512
Am I missing anything?

local function GetAllSpawns(baseplate)
		local startPosition = baseplate.Position - Vector3.new(baseplate.Size.X / 2, 0, baseplate.Size.Z / 2)
		local positions = {}
		
		for i = 1,(baseplate.Size.X / size) do
			for j = 1,(baseplate.Size.Z / size) do
				table.insert(positions, startPosition + Vector3.new(i, 1, j))
			end
		end
		
		return positions
	end

That is the correct number of spawns for an increment of 1 since 512 * 512 = 262144.

Oh right, I should have mentioned I was attempting to only get the top layer stud of a baseplate.

(crashed studio while I was attempting to spawn all the bricks lol)

You would likely also want to add Vector3(0,baseplate.Size.Y / 2 + (2 or 3), 0) to get the position on top of the baseplate. I would also recommend you use an increment of 10+ since an increment of 1 has a lot of possible positions. So your variable ‘size’ in this case is the increment I’m talking about.

1 Like

For this, you don’t need to make a table of all positions on the top surface by stud.

Instead, you can do something like:

local function randomPointOnTop(part)
	local x = math.random(-part.Size.x * 0.5, part.Size.x * 0.5) -- random x point
	local z = math.random(-part.Size.z * 0.5, part.Size.z * 0.5) -- random z point
	return part.CFrame * Vector3.new(x, part.Size.y * 0.5, z) -- point on top of the part
end
5 Likes