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;
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
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
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
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.
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