# How to random position for part in part

Hi!

I want to know, how i can random position inside part?
i have try to use math.random() but it outside part

Thanks! and sorry for bad english.

3 Likes

You need to study Vector3âs.

Hereâs an example:

``````local part = Instance.new("Part", workspace)
part.Position = Vector3.new(math.random(10), math.random(10),math.random(10))
``````
2 Likes

Ummm i mean i wanna get random position inside part for part

1 Like

Depends on what is the size of the part.

1 Like

Oh simply use constraints for your random position:
Basically what you will want to do is set your âdefault pointâ at your parts Vector3 position then use the size of each of the partâs vertices in your math.random().

1 Like

Uhh sorry i donât understandâŚ

1 Like

https://i.gyazo.com/40043602a0b0aaf618194943edd8d317

``````    local Part = game.Workspace.Part

local PartMoving = game.Workspace.PartMoving

PartMoving.CFrame = Part.CFrame * CFrame.new(math.random(-Part.Size.X/2, Part.Size.X/2),math.random(-Part.Size.Y/2, Part.Size.Y/2),math.random(-Part.Size.Z/2, Part.Size.Z/2))
``````
14 Likes

This will position the part in the center of the outside part like so

then it will add the size of the outside brick divided by 2 like so

this should work for what you need but it doesnât account for the size of the inside brick

``````NewPart.CFrame = OutSizePart.CFrame*CFrame.new(Vector3.new(math.random(-(OutSidePartSize.X/2), OutSidePartSize.X/2), math.random(-(OutSidePartSize.Y/2), OutSidePartSize.Y/2), math.random(-(OutSidePartSize.Z/2), OutSidePartSize.Z/2))
``````
2 Likes

Its a bit similar to the solution in this thread Random gems spawn

There are many people are answered, i donât know i will mark who as solution.

I believe it gets even more complicated if youâre working in negative quadrants so youâll need to include the absolute value of some variables.

if you go off the cframe of the first part it doesnât matter if itâs negative or not

Yeah I just saw your answer, it should be the proper solution!

But i wanna use MoveTo() i donât know how i can use your solution because iâm confused.

You are moving a humanoid to a random position ?

No iâm doing random meteor falling position i think iâm ok now but i donât know how to convert CFrame to Vector3

Simply:

``````local x = CFrame.new(1,2,3)
print("CFrame = "..x)
local y = x.p
print("Vector3 = "..y)
``````

Code on that line:

``````m:MoveTo( CFrame.new( Part.CFrame * CFrame.new(math.random(-Part.Size.X/2, Part.Size.X/2),math.random(-Part.Size.Y/2, Part.Size.Y/2),math.random(-Part.Size.X/2, Part.Size.X/2))).Position )
``````

Doesnât include the MoveTo but it is what it is Make blocks fall in random positions within the area of a block