Randomizing position

Hello, So I have this script which makes parts randomly fall from the sky however I’m trying to make it so that I can drag this script into any part and expand the part and it will generate the parts to a random position within the part the script it’s located in. If that makes any sense. So Heres the script I don’t know much about vector3.random or stuff like that so any suggestions would be much appreciated. Kind Regards ZoomCode!


while true do
	wait(5)
	for i = 1,500 do
		local p = mesh:Clone()
		wait(.2)
  		p.Position = Vector3.new(math.random(-245,225),95,math.random(-245,225))
 		p.Parent = script.Parent
	end
end
7 Likes

One way you could do this is:

part = script.Parent
partLowX = part.Position.X - (part.Size.X / 2)
partHighX = part.Position.X + (part.Size.X / 2)
partLowY = part.Position.Y - (part.Size.Y / 2)
partHighY = part.Position.Y + (part.Size.Y / 2)
partLowZ = part.Position.Z - (part.Size.Z / 2)
partHighZ = part.Position.Z + (part.Size.Z / 2)
print(partLowX)
--Whichever variable you need to check

Place a script within a part and it will detect the outer edges by using it’s size to compare from it’s center. You should be able to chuck the necessary variables inside of the math.random.

Hope this helped! Let me know if you need further assistance!

5 Likes

instead of doing that why do you have one large invisible part, then you have invisible parts in that large part from where the part can be generated, and you would make this into a model, and put all the invisible parts in a folder in the model or another model in the model, then you just do this:

local invisibleParts  = -[[-put link to invisible parts here]]:GetChildren()
while true do 
    wait(5)
    local clone = mesh:Clone()
    local position = invisibleParts[math.random(1,#invisibleParts).Position
    clone.Parent = script.Parent
    clone.Position = position
end
4 Likes

This is a pretty simple script to make.

local Spawner = script.Parent
while wait(5) do
	for i = 1,500 do
		local p = mesh:Clone()
		local Hight = Spawner.CFrame.Y
		local XSize = Spawner.Size.X/2
		local ZSize = Spawner.Size.Z/2
  		p.CFrame = CFrame.new(math.random(-XSize,XSize),Hight,math.random(-ZSize,ZSize))
 		p.Parent = Spawner
		wait(.2)
	end
end

You’re welcome my good sir.

5 Likes

Thanks this almost works :stuck_out_tongue: The only thing it’s missing is I need the parts to all fall under it they are falling a little off that way they fall right under it so basically where ever I move the part it’ll rain parts if that makes sense but it almost works Thank you!

1 Like

You could make the part not cancollide.

1 Like

Hmm, I tried that but it still seems to be doing it I’m so close!

1 Like

You can do like this:

local position = {
    Pos1 = Vector3.new(1,1,1)
    Pos2 = Vector3.new(2,2,2)
}
local function randomPos()
    local random = math.random(2) -- More if you have more position
    local newPos = position["Pos"..random] -- EX: position.Pos1 or position.Pos2
    return newPos
end

1 Like

This should work. The math.random() function for the X Position and the Z Position will choose a random number inside the bounds of the Spawner on that axis. Basically, it’s just getting the Spawner’s Position and adding or subtracting half of the size of the Spawner from its Position (the middle of the Spawner).

local Spawner = script.Parent

while true do
	for i = 1, 500 do
		local p = mesh:Clone()
		
		local xPos = math.random(Spawner.Position.X - Spawner.Size.X/2, Spawner.Position.X + Spawner.Size.X/2)
		local zPos = math.random(Spawner.Position.Z - Spawner.Size.Z/2, Spawner.Position.Z + Spawner.Size.Z/2)
		
		p.Position = Vector3.new(xPos, Spawner.Position.Y, zPos)
		p.Parent = Spawner
		task.wait(0.2)
	end
	task.wait(5)
end
4 Likes

I would recommend adding parts on opposite corners of your part, and then taking those positions and placing those into the math.random().

2 Likes

Thanks this worked! The code worked great.

3 Likes