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
can someone help mew about this?

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

https://developer.roblox.com/en-us/api-reference/datatype/CFrame
CFrame.Position

Simply:

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

Error: > ServerScriptService.GameScript:280: invalid argument #1 to ‘new’ (Vector3 expected, got CFrame)

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