How to correctly offset based on grid

I am wanting to offset this block to the next available grid position, which works with slabs, but if I have a part that isnt as thick as a slab, it doesn’t work right.

PartPosition is the position of the part my mouse is over. GRID_SIZE = 3 and normal is the normal of the part I on. Looking in particular at the top half.
ezgif.com-gif-maker (82)

if math.floor(part.Position.Y - 0.75) % 3 == 0 then -- Top half
	return PartPosition + (normal * GRID_SIZE) + Vector3.new(0, GRID_SIZE, 0)
else -- Bottom half block
	return PartPosition + (normal * GRID_SIZE) + Vector3.new(0, GRID_SIZE, 0)
end

If I remove the + Vector3.new(0, GRID_SIZE, 0) it works with the red part, but then places the block inside the slab for slab blocks.

Both slab and red block are placed at the top of a grid space, they just different thickness’ So ideally the code should be able to just figure out the next available slot

1 Like

ur using the wrong formula the formula of snapping is

math.round((Size/GridSize + 0.5)*GridSize)

That returns a number, I need to return a vector3 (the position the transparent block should appear)

then make it into a vector3 like this

local function Snap(GridSize,POS)
  local X = math.round((POS.X/GridSize + 0.5)*GridSize)
  local Y = math.round((POS.Y/GridSize + 0.5)*GridSize)
  local Z = math.round((POS.Z/GridSize + 0.5)*GridSize)
  return Vector3.new(X,Y,Z)
end

Snap(GridSize,Position) -- replace GridSize with yo GridSize and Position with yo position

No, I am trying to make a block based game, no different to islands and bedwars

looks like minecraft but anyways end of convo see the code try it out

This is why I have to do some more advanced coding logic to get position
image
image

wdym by that? please explain a but more

Well can see block is way off from where it actually should be

hmm then i did made a script that detect the surface of the part and adds the size now i dont have it rn so yea sorry but you can always make it

Yes, that is what I am trying to do…

now i am clue less if even after that it dosent work

Sorry to continue this pointless conversation but for the slab couldn’t you just put an invisible cube to offset it by, then you could skip all the extra math and just do the classic method with the invisible cube that’s hopefully welded to the slab.

I’m unsure what you mean by this??