Rounding Mouse.Hit Positions is Unreliable

Hey there.

I am using a building placement system for a game I am creating. This requires me to be able to snap the placement of buildings to fine positions. By default, the grid size is set to 0.25 studs. So, anywhere in the below code that you see “gridSize”, just replace that with 0.25.

This is the code in question:

local roundedPosition = Vector3.new(
	math.floor((hit.X + gridSize / 2) / gridSize) * gridSize,
	math.floor((hit.y + gridSize / 2) / gridSize) * gridSize + script.Parent:FindFirstChildOfClass("Model").PrimaryPart.Size.Y/2,
	math.floor((hit.Z + gridSize / 2) / gridSize) * gridSize
)

Essentially, it tries to take your mouse’s hit position and round it to a number. This works great, and produces a desired snapping result.

However, at lower increments (such as the default 0.25), it is seemingly not calculated the same between the server and client.

An example is how the Client registered the roundedPosition at:
-7.25, -459.75, 7 - Client
Whereas the exact same code, without moving my mouse, calculated the roundedPosition at:
-7.199999809265137, -459.70001220703125, 7 - Server

Or, from another example:
-1.25, -459.75, 7 - Client
-1.2000000476837158, -459.70001220703125, 6.900000095367432 - Server

I can promise with absolute certainty that the code is the exact same on the Server and Client scripts.

Is there any reason why this is happening?

An easier solution, in my opinion for this, would be something like this (example for Z):

math.floor(hit.Z/gridSize)*gridSize

I don’t know why you are dividing the grid size by 2 and adding that to the position. If you are still having issues, I don’t know what it would be, but you would probably have to send the scripts for it to be solved by us.