Random Part Positions Keep Ending Up In The Same Vicinity

Hello, I’m trying to make a script that places parts in a random area of this green part

However, they’re all being placed on one side of the area.

Here’s the script

	for i = 1,100 do  wait(0.3)
	local rock1 = game.ReplicatedStorage.Items.Rock:Clone()
	local rock2 = game.ReplicatedStorage.Items.Rock:Clone()
	local rock3 = game.ReplicatedStorage.Items.Rock:Clone()
	for i,v in pairs (rocks) do
	local x = math.random(1,game.Workspace.Region.Position.X)
	local y = math.random(1,game.Workspace.Region.Position.Y)
	local Z = math.random(1,game.Workspace.Region.Position.Z)
	v.Position = Vector3.new(x,y,Z)
	v.Parent = game.Workspace
	v.CFrame = v.CFrame * CFrame.Angles(0.5,0,0)
	for i,v in pairs (rocks) do
        ---making a rocketpropulsion
		local rockprop = Instance.new("RocketPropulsion",v)
		rockprop.Target = orb
		rockprop.MaxSpeed = 450000
		rockprop.MaxThrust = 450000
		rockprop.TurnD = math.random(200,300)
		rockprop.TurnP = math.random(200,300)
		v.Anchored = false
		local rockpropreached = script.RockPropReached:Clone()
		rockpropreached.Parent = v
	local position = table.find(rocks,rock1)
	local position2 = table.find(rocks,rock1)
	local position3 = table.find(rocks,rock1)
    ---removing all the parts from the table

So… what’s your question then? What are you trying to achieve? How does your current implementation not meet standard? What should it look like?

The current implementation only places parts in a random area in a small vicinity of the green part, however I want it all over the green part.

Currently, your script only allows for the minimum xyz values to be 1. This means that your parts will only appear in one direction of the green part.

An example of this behavior:

Notice how the dots are only appearing in one direction from the center, which is the one with only positive X and Y values.

To fix this, you can set your minimum to be the negative of your maximum in math.random.

For example:

local x = math.random(1,game.Workspace.Region.Position.X)

Would become:

local x = math.random(-game.Workspace.Region.Position.X,game.Workspace.Region.Position.X)

Hello, I changed the minimum of X and Y to be the negative of my maximum, however, this seems to have the same effect.

local x = math.random(-game.Workspace.Region.Position.X,game.Workspace.Region.Position.X)
local y = math.random(-game.Workspace.Region.Position.Y,game.Workspace.Region.Position.Y)


I just now noticed you were only using the region’s position. This doesn’t make very much sense, as the area you need to place it in depends on the size and position of the green part.

Try using this instead:

local region = game.Workspace.Region
local size = region.Size / 2
local x = math.random(-size.X,size.X)
local y = math.random(-size.Y,size.Y)
local z = math.random(-size.Z,size.Z)
v.Position = region.CFrame * Vector3.new(x,y,z) -- Multiply the region's CFrame with the size to position it

(This isn’t tested, so I may have done the CFrame part wrong)

Thanks it worked! :grinning: :smiley:

1 Like