I'm trying to make, let's just say, rain. So I have an invisible block called DropZone and I want it so the "rain" drops ONLY in random positions that are in that block, so that the "rain" won't spawn randomly in the entire world, but only in the boundaries of this block. I've tried multiple different things, but to no avail. Here is the two scripts I tried.
newTeapot.Position = Vector3.new( math.random( -dropZone.Position.X * 2, dropZone.Position.X * 2 ), dropZone.Position.Y, math.random( -dropZone.Position.Z * 2, dropZone.Position.Z * 2 ) )
newTeapot.Position = Vector3.new( math.random( -dropZone.Position.X / 2, dropZone.Position.X / 2 ), dropZone.Position.Y, math.random( dropZone.Position.Z / 2, -dropZone.Position.Z / 2 ) )
You're trying to get a random position along the top surface of the part. The randomness then is in scale with the part's Size
, not Position
.
local relativeTopSurface = Vector3.new(math.random() - 0.5, 0.5, math.random() - 0.5) * part.Size
This is a position that is relative to part
. If part
is facing the default way (Front is -Z, Top is +Y, etc) you can just add this to the position:
local randomAbsoluteOnTop = part.Position + relativeTopSurface
If the part is rotated somehow, that won't work. But it's easy to fix:
local randomAbsoluteOnTop = part.CFrame * relativeTopSurface -- above expression is equivalent to = part.CFrame:pointToWorldSpace(relativeTopSurface)