Block grid placement help

I have this block of code which makes a block follow your mouse in a grid

local runservice = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local block = game.Workspace:WaitForChild("Block")
local camera = game.Workspace.CurrentCamera
local cameraPart = game.Workspace.camerapart

local gridSize = 3

local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Exclude
castParams.FilterDescendantsInstances = {block, cameraPart}

local function roundToGrid(n, grid)
	return math.floor((n + grid / 2) / grid) * grid
end

local function followMouse()
	local mouseLocation = UserInputService:GetMouseLocation()
	local unitRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
	local cast = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, castParams)

	if cast then
		local x = roundToGrid(cast.Position.X, gridSize)
		local z = roundToGrid(cast.Position.Z, gridSize)
		

		block.Position = Vector3.new(x, 2.5, z)
	end
end

And I have a grid pattern on the floor of my game. I want my block to follow the grid pattern. my pattern looks like this:
image

And as you can see my part is not following the grid pattern:
image

Is there i way i can fix this?

just offset ur grid pattern by half tile

that doesnt work is there any other way

by doing the same on grid formula, multiply grid size by 0.5

return math.floor((n + grid * 0.5) / grid * 0.5) * grid
this should do it probably

now the block is just offsetted from my mouse??

image

return math.floor((n + grid / 2) / grid + 0.5) * grid
this perhaps

image
still, doesnt work

return math.floor((n + grid / 2) / grid + 0.5) * grid

image

I dont think this whole approach is gonna work

There’s nothing wrong with your grid alignment code to begin with. the problem is that those black & white tiles themselves aren’t aligned to the grid. You need to move those tiles so that they are aligned.

bruh then go and do basic worst math and do this
return math.floor((n + grid / 2) / grid + 0.5) * grid + grid / 2

Would there be a way to do something else? like make the part relative to the tiles position or something like that or is that the only way

You could try getting the tile at the top left corner, converting the mouse position to a location relative to that tile (tile.CFrame:PointToObjectSpace(cast.Position)), align that position to the grid, and then convert it back to world space (tile.CFrame:PointToWorldSpace(alignedPosition)).

why don’t you use mouse.Target?

local players = game:GetService("Players")

local player = players.LocalPlayer
local mouse = player:GetMouse()

local tilesFolder = --Folder with your tiles
local tileSize = tilesFolder:FindFirstChildOfClass("Part").Position.X
local heightOffet = CFrame.new(0, (tileSize.X/ 2) + (tileSize.Y/ 2), 0)

local blockPart = Instance.new("Part")
blockPart.Anchored = true
blockPart.CanCollide = false
blockPart.Size = Vector.new(tileSize, tileSize, tileSize)
blockPart.Parent = workspace


mouse.Button1Down:Connect(function()
--
local target = mouse.Target
--
if target.Parent ~= blocksFolder then return end
--
blockPart.CFrame = target.CFrame * heightOffset
--
end

(Created on mobile, errors/ bugs could be present)