How to make a part spawn randomly in a part

So basically I want a part to spawn randomly in a certain place which is a part

1 Like

Could you maybe explain what you mean in more detail? You can use math.random() for randomness. If you’re looking to place in a random position relative to another part, you’ll have to use an offset method such as Vector addition or CFrame multiplication operations.

Ok, I want to make a pet for example with is a mesh part to be placed anywhere in a base part

Do you want it to be placed somewhere within the range of a base part or just to become the child of one? If it’s the former, how accurate do you want it to be?

I want it to be placed within the range of it

If accuracy isn’t a huge issue, then you can just use an offset of the given part.

local range = 10
Part1.Position = Part2.Position + Vector3.new(math.random(-range,range),math.random(-range,range),math.random(-range,range))

Or the CFrame version:

local range = 10
Part1.CFrame = Part2.CFrame * CFrame.new(math.random(-range,range),math.random(-range,range),math.random(-range,range))

You can replace range with the size or however you want to determine the possible range. If you want it exclusively inside of the part, you’ll either have to do some maths or use something like Region3 or RotatedRegion3.

1 Like

Ok thank you really appreciated

1 Like