How to place part on top of another part with script

Hi! Today I’m going to show you how to place a part on top of another part. This works even if the you change the size of the part you want to put on top of another part.

(NOTE: This is only for people new to Vector3 and is not targeted to people with much knowledge about it. )
(NOTE 2: This is a short tutorial and also my first topic. I would likely appreciate it if you give feedback. )

This is the code that I made which I’m about to explain:

local partA = game.Workspace:WaitForChild("PartA")
local partB = script.Parent

partA.Position = partB.Position + Vector3.new(0,(partB.Size.Y/2 + partA.Size.Y/2),0)

Rough sketch of partA and partB:

So basically what this does is setting the partA (The part you want to put on top of another part) 's position to partB (The part below the partA) 's position then adding a Vector3 to it. As you can see, the Vector3 's Y axis is the only one with a value. This is because we’re only talking about the Y axis a.k.a the top and the bottom of the part. The value in the Y axis is the sum of the half of the Y of partB 's size and the half of the Y of partA 's size, which is 5. Why do we need to divide the Y axis of the size of both parts before adding them? This is what happens when you don’t divide them:

As you can see there’s a large gap between the two parts. That’s because we don’t divide the Y axis of the size of both parts before adding them. This is what happens when you do it correctly:

Thanks for reading it and hope it helps! Feel free to give feedback. :smiley:

9 Likes

Using CFrame would be a wayy better option actually as it gets the exact top of the part

1 Like

Thanks for the feedback! I’m using Vector3 since CFrame may be complex to new people learning about scripting.

CFrame is not complex unless you wanna go mumbo jumbo mode

1 Like

You’d have to do the exact same calculations with CFrame as well. The only advantage you’d gain is that if partB was rotated partA would still be perfectly placed on top.

local partA = workspace:WaitForChild("PartA")
local partB = script.Parent

partA.CFrame = partB.CFrame * CFrame.new(0, (partB.Size.Y/2 + partA.Size.Y/2), 0)
2 Likes

Never said you didn’t know.

I was replying to your statement about how CFrame would’ve been a way better option because

Which is not quite right so just made that clear.

1 Like

I appreciate the feedback! But please don’t start an argument since I already understood what kidsteve923 said.

1 Like

Very nice job, I am sure beginners will find this helpful for them!

3 Likes