How to get the coordinates of a part

Greetings,

The title might be misleading, but I am looking to get the coordinates of a part from within it. Basically, I have a script that does math.random and it chooses 2 positions randomly based on the position from the part. Most of the time, the coordinates that are chosen are outside of the part itself. How do I make it so that it only chooses coordinates within it? Thanks!

1 Like

This might work, i havent tested it

print(Part.Position + Vector3.new
(math.random(-Part.Size.X, Part.Size.X),
math.random(-Part.Size.Y, Part.Size.Y),
math.random(-Part.Size.Z, Part.Size.Z))
1 Like
3 Likes

The answer is rather trivial simple!
Basically what you do is create a random cframe inside of a part, if it wasn’t modified.
Then you get the relative cframes and update the point’s cframe to the of the parts.

But how to subtract in CFrames?
Well you inverse them and then perform the add operator function like this.

local cf1 = ...
local cf2 = ... --random
local relativeCFtocf1 = cf1 * cf1:Inverse()

It gives a better result, but still, it isn’t there. I am going to post a screenshot below to show the problem.

The parts that you see in the screenshot are supposed to be spawned only on the concrete road, not on the sidewalks, and not on the grass. There are 5 quarters that are for each street. It is chosen randomly each time a new part spawns. The problem is, that the parts still spawn outside of the specified region even after the quarter has been chosen. Please help!

EDIT:
This is the script for the random quarter selection with the position lines:

I couldn’t attach the lines directly since it wouldn’t align them and they would but like a word document :expressionless:

Just moving the post to the top page because I still need help

local part = workspace.Part

local rand = Vector3.new(
	math.random(-100,100)/100 * part.Size.X/2,  --math.random is now a capped value between -1 and 1, which is the multiplied by half the size of the Axis.
	math.random(-100,100)/100 * part.Size.Y/2,
	math.random(-100,100)/100 * part.Size.Z/2
)

local positionWithinPart = part.Position + rand;

The problem now is that they only spawn in one-quarter out of 5 and they do still spawn outside of the area sometimes.

Image explanation:

Basically, the parts have to spawn randomly on multiple streets as shown.
local quarter = math.random(1,5)

Script:

you’re not positioning the parts relative to the chosen quarter.

You need to make the position:

chosenquarter.Position + Vector3.new(pos1, 2, pos2)
1 Like

Sadly they do still spawn outside :frowning:

They also have a specific pattern that they use to spawn outside hmmm

not exactly sure what the issue is then, sorry.

1 Like

Moving the post to the top of the front page since I still need help with this issue.