Spawning parts onto certain other parts

So what I’m trying to achieve here is make Blue appear near Red, but only on top of Yellow, like this.

Blue can appear anywhere on top of Yellow, as long as it’s near Red. I’m completely unsure of how to do this. I can make Blue appear directly on top of Yellow’s position, but that obviously isn’t what I’m aiming for. Also, I’d like to know how to make Blue appear near Red as long as it, Blue, isn’t going to be on Yellow (just switch where Red and Blue are for a visual example). Another problem I have is knowing, if there are two Yellow parts, which one is closer, since Blue should appear on the closer one. Again, I’m not sure how to do this, so there isn’t any coding done, but my best guess is that this has something to do with direction in a CFrame or Position.

Your wording was a bit complicated, but I assume you want to have a blue part appear on the yellow part as close to the red as possible(while still being on the yellow part? Is this correct? (In your statement you say that blue can appear anywhere on yellow as long as it’s close the red, so would you want like a math.random() limited to a certain distance away from red?)

for finding out which yellow part is closer, you could just do .Magnitude on the position of every yellow part compared with the red part. For more information on .Magnitude, visit this:

If you cast a unit ray going in the same direction as the yellow part, then you can use Ray:ClosestPoint() and then input the position of the red part as the parameter for Ray:ClosestPoint(). You can find the wiki’s article on Rays at Ray | Documentation - Roblox Creator Hub. Upon doing this, The function returns the closest Vector3 that is also on the path of the ray. Now all you have to do is move the blue part to that point, and change the Y value so that the part goes on top of Yellow, and not inside of it. Sorry if that was worded really awkwardly, I’m not really sure how to describe it better.