Move my part on top of other part without MoveTo()?


#1

I’m trying to automatically add grass above a part w/ the command bar. The problem is my calculations seem to be wrong. I’m simply cloning the part I want it to appear on, making it smaller, and then setting its CFrame above the part. The problem is, I can’t seem to figure out how to get it directly on top of my part.

local g = game.Selection:Get()[1]

local cl = g:Clone()
cl.Size = Vector3.new(cl.Size.X, .2, cl.Size.Z)
cl.CFrame = g.CFrame + Vector3.new(0, g.Size.Y/2 + cl.Size.Y, 0)
cl.Parent = game.Workspace`

My code offsets the part by a small amount instead of putting it directly on top of the other one.

I essentially want to recreate MoveTo(), but with CFrame. For this one part. I hope that makes sense. This is my desired result:

some%20say

Any help is appreciated!


#2

It seems like you have the base formula there. I mean, I will say it’s a bit hard to infer which variables are what, but everything looks good, from what I know atleast.

One idea might be to create an offset, and then use/save that offset for when ever you add grass to a part. That might work??


#3

your code is right you’re just missing the -.1 to account for the new top part

local g = game.Selection:Get()[1]

local cl = g:Clone()
cl.Size = Vector3.new(cl.Size.X, .2, cl.Size.Z)
cl.CFrame = g.CFrame + Vector3.new(0, g.Size.Y/2 + cl.Size.Y - cl.Size.Y / 2, 0)
cl.Parent = game.Workspace


#4

Awesome! That worked like a charm. Thank you so much!


#5

You could also write

cl.CFrame = g.CFrame + Vector3.new(0, g.Size.Y/2 + cl.Size.Y/2, 0)

#6

oops I didn’t even see that he added cl.Size at all in the first place, I just saw that it was off by .1 in studio and subtracted