Grid system poor functionality

Hello everyone! I copied a grid calculation thingy from another code for a placement system, yet a small problem arose.

local PosX = math.floor(position.x / self.grid + 0.5) * self.grid
local PosY = math.floor(position.y / self.grid + 0.5) * self.grid -- the problem
local PosZ = math.floor(position.z / self.grid + 0.5) * self.grid

The problem is that, let’s say that the grid size is 4, so 4 studs. If I were to place my object on the ground, it would not only follow the grid X and Z grid (which works fine), but it will then be 4 studs off the ground…? That’s not what I want!

Another thing that might deem helpful, is that I am using the mouse position here. So position = Mouse.Hit.X, Mouse.Hit.Y and Mouse.Hit.Z.

Any pro math-people please help out, I am already tired of doing math for like 3 hours now…

local PosY = math.floor(position.y / self.grid + 0.5) * self.grid -- the problem

Yes. This line of code is very much the problem. It’s entire purpose is to calculate the snap position for the Y axis. If you don’t want that, don’t do it. Place the assembly at the Y-level of the mouse, offset by half the vertical size of the assembly