Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trying to make a part randomly generate in specific region?

I have a 16x1x16 base that I want 2x2x2 parts to randomly appear on specifically only in the 16x1x16 area of the part.

2 answers

✓ Accepted answer

You should try doing it yourself first, and if you can't get it to work then post your code here for others to help with, but this does what you've asked:

local basePart = workspace.BasePart -- Set this to your 16x16 base part
local basePosition = basePart.Position
local baseSize = basePart.Size

-- Calculate spawn height, as well as min and max X and Z based on basePart size and position
local spawnHeight = basePosition.Y + (baseSize.Y / 2) + 1
local minX = basePosition.X - (baseSize.X / 2) + 1
local maxX = basePosition.X + (baseSize.X / 2) - 1
local minZ = basePosition.Z - (baseSize.Z / 2) + 1
local maxZ = basePosition.Z + (baseSize.Z / 2) - 1

-- Create 2x2 part here, or just set partTemplate to your existing 2x2 part
local partTemplate = Instance.new("Part")
partTemplate.Size = Vector3.new(2, 2, 2)
partTemplate.Anchored = true
partTemplate.CanCollide = false
partTemplate.Color = Color3.new(1, 0, 0)

-- Specify how many parts we want to spawn
local numParts = 12

local rng = Random.new()

-- Go through loop for every part we want to spawn, cloning the template and setting it's position with random X and Z
for i = 1, numParts do
	local part = partTemplate:Clone()
	local newX = rng:NextNumber(minX, maxX)
	local newZ = rng:NextNumber(minZ, maxZ)
	part.Position = Vector3.new(newX, spawnHeight, newZ)
	part.Parent = workspace
end
while true do
wait(1)
local P = Instance.new("Part")
P.Size = Vector3.new(2,2,2)
P.Position = Vector3.new(math.random(minx,maxx),0,math.random(minz,maxz)
end)

Change minx and maxx to the left and right position of the baseplate. Change minz and maxz to the top and bottom position of the plate this works for any baseplate change the wait to the interval between spawns enjoy :)

This discussion is preserved as it was. New answers, comments, and votes are closed.