How would I make parts spawn at random in a specific area?

Hello, Scripting Support. I require your help.

My goal is to generate a large amount of parts in this specific area:
image

The parts will come in two different sizes (2x2x2,3x3x3) and they should not be overlapping with eachother. The problem is that I have no idea how I would go about this.

Any help would be much appreciated. Thank you.

This has a little bit of math, but it’s easy if you do it correctly.

Try this:

local bounds = workspace.Part --Redirect this to your part

for i = 1,50 do
    
    local newPart = Instance.new("Part", workspace)
    newPart.Name = tostring(i)
    newPart.Anchored = true
    newPart.Size = Vector3.new(1,1,1)
    newPart.Position = Vector3.new(math.random(bounds.Position.X-(bounds.Size.X/2),bounds.Position.X+(bounds.Size.X/2)),math.random(bounds.Position.Y-(bounds.Size.Y/2),bounds.Position.Y+(bounds.Size.Y/2)),math.random(bounds.Position.Z-(bounds.Size.Z/2),bounds.Position.Z+(bounds.Size.Z/2))
    
end

Hey! I just found my own solution, one that works perfectly. Thanks for your input though!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.