How would I snap to a grid with Raycasts?

Here’s my raycast code. Re-used code from my old games.

    local ignore = {game.Players.LocalPlayer.Character, game.Workspace.CurrentCamera.Viewmodel, selection}

	local ray = Ray.new(
		game.Workspace.CurrentCamera.CFrame.p,                          
		(mouse.Hit.p - game.Workspace.CurrentCamera.CFrame.p).unit * 500
	) 
	local part,position,normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)

How would I add a grid / placing system to this. I already have a destroying system.

Also, although this isn’t the answer

This function has been deprecated. Use WorldRoot | Roblox Creator Documentation along with RaycastParams | Roblox Creator Documentation for new work.

I have no idea how to use it. I’ll figure out soon.

I was confused at first but it’s pretty similar to the game.Workspace:FindPartOnRayWithIgnoreList

Okay but still. How does it work?

Also. Can I detect a part with it?

It’s just a new verison of a ray in a way.

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character, game.Workspace.CurrentCamera.Viewmodel, selection}
raycastParams.IgnoreWater = true -- change this to what you want

local raycastResult = nil
raycastResult = workspace:Raycast(game.Workspace.CurrentCamera.CFrame.p, (mouse.Hit.p - game.Workspace.CurrentCamera.CFrame.p).unit * 500) -- change this
if raycastResult then
local part, position = raycastResult.Instance, raycastResult.Position

Yeah, I not sure if the code above even works but uhh yeah. I’m sorry I don’t have the exact answer for your questions. But change the code above to your situation

It isn’t. I’m just trying to make a build system.

There is a formula:

local gridded = math.floor(x / gridSize) * gridSize


-- example:
local function gridPosition(position, gridSize)
   local x = math.floor(position.X / gridSize) * gridSize
   local y = math.floor(position.Y / gridSize) * gridSize
   local z = math.floor(position.Z / gridSize) * gridSize

   return Vector3.new(x, y, z)
end

gridPosition(Vector3.new(3, 4, 5), 2)  -- Vector3.new(2, 4, 4)

How would I get a value back? Sorry I’m new.

-- Your code:
local ray = Ray.new(
		game.Workspace.CurrentCamera.CFrame.p,                          
		(mouse.Hit.p - game.Workspace.CurrentCamera.CFrame.p).unit * 500
	) 
	local part,position,normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)


local function gridPosition(position, gridSize)
   local x = math.floor(position.X / gridSize) * gridSize
   local y = math.floor(position.Y / gridSize) * gridSize
   local z = math.floor(position.Z / gridSize) * gridSize

   return Vector3.new(x, y, z)
end

-- Getting the gridded position
ObjectYourePlacing.Position = gridPosition(position)

How would I place it in the direction that they put their mouse though?

I think I phrased it wrong. I meant put it in the direction and make it align with the block that it was placed on.

Solved. Here’s what I used!

block.CFrame = CFrame.new(position,position + normal)
block.Position = part.Position
block.CFrame = block.CFrame * CFrame.new(0,0,-4)
block.Orientation = Vector3.new(90, 90, 0)

A really simple bit of code that works effectively.