Math.floor and math.ceil behavior

I built a functioning building tool (I will make the model free) but before I use it and make it free, there is a minor issue.

I wanted to make the building tool to have a snap when you move the mouse to a different position or should I say, “grid” and, so I did using math.floor and math.ceil. (It didn’t work)

robloxapp-20200614-1829419.wmv (1.2 MB)

So if I use math.ceil, for the top, back, and right surfaces glitch out of the bottom block below them, while using math.floor does it for the bottom, front, and left surfaces.

I cutted the code into an important part, so here you go:

while true do
	wait()
			
	local divided1 = mouse.Hit.x / 4
	local rounded1 = 4 * math.ceil(divided1)
	local divided2 = mouse.Hit.y / 4
	local rounded2 = 4 * math.ceil(divided2)
	local divided3 = mouse.Hit.z / 4
	local rounded3 = 4 * math.ceil(divided3)
			
	-- math.ceil
			
	if mouse.TargetSurface == Enum.NormalId.Top then
		mainBlockCopy.CFrame = CFrame.new(rounded1,rounded2,rounded3)
	end
			
	if mouse.TargetSurface == Enum.NormalId.Back then
		mainBlockCopy.CFrame = CFrame.new(rounded1,rounded2,rounded3)
	end
			
	if mouse.TargetSurface == Enum.NormalId.Right then
		mainBlockCopy.CFrame = CFrame.new(rounded1,rounded2,rounded3)
	end
			
	-- math.floor
			
	if mouse.TargetSurface == Enum.NormalId.Bottom then
		mainBlockCopy.CFrame = CFrame.new(rounded1,rounded2,rounded3)
	end
			
	if mouse.TargetSurface == Enum.NormalId.Front then
		mainBlockCopy.CFrame = CFrame.new(rounded1,rounded2,rounded3)
	end
			
	if mouse.TargetSurface == Enum.NormalId.Left then
		mainBlockCopy.CFrame = CFrame.new(rounded1,rounded2,rounded3)
	end
			
end

If you’re confused, I get it. I will try to explain again but more better.

Try adding a .5 to your rounded numbers so the center of the part goes to the mouse rather than the edge, so…

 ( math.floor( divided[number]  + .5 )  * 4
 ( math.ceil( divided[number]  - .5 )  * 4
1 Like

Thanks! When I changed the script and made all of the parts into the ground but that’s easy to fix.