Help with block placement system

I tested my Block placement tool and then I noticed a problem with it. I couldn’t build on some sides. Here is a video of what I am talking about.
roblox.wmv

Also, here is the script I am using for the tool.

client script:

local player = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()
local off = false
local tool = script.Parent
local equipped = false
local posx
local posy
local posz

local gridsize = 4

local function snap(clonedbrick)
	posx = math.floor(mouse.Hit.X/gridsize + 0.5) * gridsize
	posy = math.floor(mouse.Hit.Y/gridsize + 0.5) * gridsize
	posz = math.floor(mouse.Hit.Z/gridsize + 0.5) * gridsize
end


tool.Equipped:Connect(function()
	if equipped == false then
		equipped = true
		local clonedbrick = game.ReplicatedStorage.Brick:Clone()
		clonedbrick.Parent = workspace
		mouse.TargetFilter = clonedbrick
		mouse.Move:Connect(function()
			snap()
			clonedbrick.Transparency = .5
			while mouse ~= nil do
				clonedbrick.CFrame = CFrame.new(posx,posy+clonedbrick.Size.Y/2,posz)
				wait()
				if off == true or equipped == false then
					clonedbrick:Destroy()
					off = false
					break
				end
			end
		end)
	end
end)

script.Parent.Activated:Connect(function()
	print("Activated!")
	local BrickToPlace = game.ReplicatedStorage.Brick:Clone()
	local pos = Vector3.new(posx,posy + BrickToPlace.Size.Y/2,posz)
	local distance = (pos - player.Character.HumanoidRootPart.Position).Magnitude
	if distance <= 12 then
		script.Parent.RemoteEvent:FireServer(pos)
	end
end)

script.Parent.Unequipped:Connect(function()
	if equipped == true then
		equipped = false
		off = true
	end
end)

Server Script:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player,pos)
	local BlocksFolder = game.Workspace.Blocks
	local BrickToPlace = game.ReplicatedStorage.Brick:Clone()
	BrickToPlace.Parent = BlocksFolder
	BrickToPlace.CanCollide = true
	BrickToPlace.Position = pos
	BrickToPlace.Name = player.Name
end)

I believe that you can use workspace:RayCast and then you will get the side of the part the raycast hit and add the half the size of the hit part and half the size of the part your moving. You can raycast in the direction of the mouse with camera:ScreenPointWithRay(). There is a topic similar to this: Placement System For Wall Objects

More information here: