For example, if I had a part as a base, how would I make it so that I can spawn smaller parts anywhere within the perimeter of the base?
The position of a part is located in the very center. Relative to the part, this is also known as Size:{X/2, Y/2, Z/2}
. To get the amount of space it takes up on a 2D plane, you would simply add and subtract these dimensions from the part's position to get where it starts and where it ends. Remember, the position starts in the middle.
-- Get the Base's position and size -- This is assuming 'base' is defined local pos = base.Position local size = base.Size -- Get the half-way point of both xz dimensions local x_half = size.X/2 local z_half = size.Z/2 -- Get the range of both dimensions -- This will return where X begins and ends, and where Z begins and ends. local x_vec = Vector2.new(pos.X - x_half, pos.X + x_half) local z_vec = Vector2.new(pos.Z - z_half, pos.Z + z_half)
Now you can randomly choose between these ranges to get any point within the base's 2D scale. For example:
-- Let's make the code above into a function local function get2DScale(part) local pos = part.Position local size = part.Size local x_half = size.X/2 local z_half = size.Z/2 local x_vec = Vector2.new(pos.X - x_half, pos.X + x_half) local z_vec = Vector2.new(pos.Z - z_half, pos.Z + z_half) return x_vec, z_vec end -- Assuming 'part' and 'base' is defined local baseVecX, baseVecY = get2DScale(base) part.CFrame = CFrame.new( math.random(baseVecX.X, baseVecX.Y), math.random(x), -- Whatever you want your height to range between math.random(baseVecY.X, baseVecY.Y) )
This would randomly position a part somewhere on that base grid. Hope that helps, if you have any questions, just let me know.