How to make A Part Appears in a random position

How could I make these Mesh Parts Spawn in a random position, the red box is where they should spawn but with different positions and they should not go to the blue box

image

You can do something along the lines of:

positions = {
Vector3.new(0,0,0),
Vector3.new(0,0,0),
Vector3.new(0,0,0),
Vector3.new(0,0,0)
}
part.Position = positions[math.random(1,4)]
2 Likes

You could loop through all of the parts and use a function like this:

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

local x = part.Size.X/2
local y = part.Size.Y/2
local z = part.Size.Z/2

local function spawnpart(Part)
      Part.Position = part.Position + Vector3.new(math.random(-x, x), math.random(-y, y), math.random(-z, z))
end
2 Likes

That would be an option, but it’s not what I’m looking for

Set their position as a random number made out of the edges coordinates.

Example:

part.Position = Vector3.new(math.random(edge1PosX, edge2PosX), math.random(edge1PosY, edge2Pos), math.random(edge1PosZ, edge2PosZ))

Note that you must use opposite edges and that the first value to math.random has to be the lowest

2 Likes

This is good but you should probably rename the function so that you don’t overwrite the global function spawn() which is used to create additional threads of execution.

1 Like