Placement System Issue

I’m working on placement system, which it works fine.

But the issue is, some parts doesn’t align properly when part’s rotation is 90 or -90 degrees.

And here is the code: (it’s really big)

local playerMouse = game.Players.LocalPlayer:GetMouse()
local UIS = game:GetService("UserInputService")
local Runner = game:GetService("RunService")
local SampleBlock
local ctr = 1
local deg = 0


function createsampleblock(dir)
if SampleBlock then
	SampleBlock:Destroy()
	SampleBlock = nil
end
SampleBlock = dir:Clone()
for i,v in ipairs(SampleBlock:GetChildren()) do
	if v.Name ~= "Base" then
		v.Transparency = 0.3
		v.CanCollide = false
	end
end
SampleBlock.Parent = workspace	
end

function calculate_ray_pos(Gridsize,Building_Limit,Ignored_Parts)
local ray = Ray.new(playerMouse.UnitRay.Origin,playerMouse.UnitRay.Direction * Building_Limit) --Creates a 3D Based Ray With Looking Direction
local hit,position,nposition = workspace:FindPartOnRayWithIgnoreList(ray,Ignored_Parts) --Calculates The Position



--Return As Vector3 Type Variable--
local pos = position + nposition * 0.5
--Using The Grid Formula--

return CFrame.new(
	math.floor((pos.X/Gridsize + 0.5)*Gridsize)-(SampleBlock.Size.X/2), 
	math.floor((pos.Y/Gridsize + 0.5)*Gridsize),
	math.floor((pos.Z/Gridsize + 0.5)*Gridsize)-(SampleBlock.Size.Z/2)
)


 



end





---------------------------------------------------------------------------

UIS.InputBegan:Connect(function(key)

if key.KeyCode == Enum.KeyCode.E then
	if ctr < 4 then
		ctr += 1
		createsampleblock(game.ReplicatedStorage.Bricks:GetChildren()[ctr])
	end
end

if key.KeyCode == Enum.KeyCode.Q then
	if ctr > 1 then
		ctr -= 1
		createsampleblock(game.ReplicatedStorage.Bricks:GetChildren()[ctr])
	end
end

if key.KeyCode == Enum.KeyCode.R then
	deg += 90
end

if key.UserInputType == Enum.UserInputType.MouseButton1 then
	--if #workspace:FindPartsInRegion3WithIgnoreList( Region3.new(SampleBlock.Position - Vector3.new(2,2,2),SampleBlock.Position + Vector3.new(2,2,2)) ,{SampleBlock,game.Players.LocalPlayer.Character}, 500) < 1 then
	game.ReplicatedStorage.Communicators.PlaceBlock:FireServer(game.ReplicatedStorage.Bricks:GetChildren()[ctr],SampleBlock.CFrame)
	script.Pop:Play()
	--end
	
end

end)


createsampleblock(game.ReplicatedStorage.Bricks:GetChildren()[ctr])
   Runner.Heartbeat:Connect(function()
   SampleBlock.CFrame = calculate_ray_pos(1,64,{SampleBlock,game.Players.LocalPlayer.Character})
   SampleBlock.CFrame = SampleBlock.CFrame * CFrame.Angles(0,math.rad(deg),0)
end)

Just a suggestion, try to find the part of the code thats making the error and send just it, sending the whole code will take longer to analise it and find your problem

function calculate_ray_pos(Gridsize,Building_Limit,Ignored_Parts)
local ray = Ray.new(playerMouse.UnitRay.Origin,playerMouse.UnitRay.Direction * 
Building_Limit) --Creates a 3D Based Ray With Looking Direction
local hit,position,nposition = workspace:FindPartOnRayWithIgnoreList(ray,Ignored_Parts) --Calculates The Position



--Return As Vector3 Type Variable--
local pos = position + nposition * 0.5
--Using The Grid Formula--

return CFrame.new(
math.floor((pos.X/Gridsize + 0.5)*Gridsize)-(SampleBlock.Size.X/2), 
math.floor((pos.Y/Gridsize + 0.5)*Gridsize),
math.floor((pos.Z/Gridsize + 0.5)*Gridsize)-(SampleBlock.Size.Z/2)
)






end

I think this part having issue

Anyway, I solved this issue via using pivot part and CFrame.