Building system with grid model snapping to the ground

i made a building system and its very simple, it uses a grid system that i have found online but the problem is that i cant make the model that im building with snaps to the ground automatically, every time i try and search for this i hear people saying “use ray casts” but they dont show anything using the ray casting

heres my script currently:

local function Snap()
 local PosX = math.floor(mouse.Hit.X / Grid.Value + 0.5) * Grid.Value -- the grid value is 4
 local PosY = math.floor(mouse.Hit.Y / Grid.Value + 0.5) * Grid.Value -- the grid value is 4
 local PosZ = math.floor(mouse.Hit.Z / Grid.Value + 0.5) * Grid.Value -- the grid value is 4
 
 return PosX, PosY, PosZ	
end
	
rs.RenderStepped:Connect(function()

 local PosX, PosY, PosZ = Snap()
 Model:PivotTo(CFrame.new(PosX, mouse.Hit.Position.Y + Model:GetExtentsSize().Y / 2, PosZ) * Model:GetPivot().Rotation)
	
end)

so i thought of using the PosY returned value but that just makes the model go through the ground for some reason so the best that i could is use the

mouse.Hit.Position.Y + Model:GetExtentsSize().Y / 2

and it works fine but the problem is that the Y value does not get effected by the grid system so it will look off and it will float over the ground when its touching a wall instead of snapping to the ground

2 Likes