How to spawn a part randomly within a part

I’m trying to make a part spawn randomly within another part.

Solutions I’ve tried:
Region3 - works but It spawns in the same place and I want it to spawn in a random place.

How would I make the part spawn randomly inside another part.

Current code:

local function spawn()
       
end

while wait(5) do
   spawn()
end

So, how would I make the part spawn randomly each time that function is called within a large part?

2 Likes

What do you mean with ‘within a part’? You mean inside of it?

Supposedly you could get a random value, check if it is one value, and create the part.

local function RandomSpawn()
    local RanNum = math.random(Min, Max)
    if RanNum == Num then
        print('foo')
    end
end

yes he means inside a larger part and spawns randomly in there

-- The part you want to spawn a new part inside of
local part = workspace["Part Name"]

-- Getting the area you are allowed to spawn the part inside of (the hitbox of 'part')
local x = part.Size.X/2
local y = part.Size.Y/2
local z = part.Size.Z/2

local function spawn()
      local newPart = Instance.new("Part") -- Spawn in your part
      newPart.Position = part.Position + Vector3.new(math.random(-x, x), math.random(-y, y), math.random(-z, z)) -- In here we are setting the position to the first parts position and then randomly placing it inside the part using the constants we defined above
      newPart.Parent = workspace["Set parent here"]
end
14 Likes