Help with aligning axes of part with another part's surface using Raycasts

I have a Part and I want to align it with another Part using Raycasts. The result I want is identical to how dragged parts align when moving them in Studio.
Here’s my current script:

local RunService = game:GetService("RunService")
local Mouse = game:GetService("Players").LocalPlayer:GetMouse()
local Part ="Part")
Part.Size =, 1, 1)
Part.Anchored = true
Part.CanCollide = false
Part.CanQuery = false
Part.Parent = game:GetService("Workspace")

	local Result = game:GetService("Workspace"):Raycast(Mouse.Origin.Position, CFrame.lookAt(Mouse.Origin.Position, Mouse.Hit.Position).LookVector * 1000)
	if Result then
		Part.CFrame = CFrame.lookAt(Result.Position, Result.Position + Result.Normal) *, 0, -0.5)

This works fine until the surface is rotated on a different axis other than the one the Part is being aligned to. Here’s an example of what I mean:

External Media
1 Like

I’m not sure if this’ll work but try subtracting it by the result’s normal instead.

It just reverses the direction of the Part, which makes it go inward.

Then you could try using the regular lookat() but then multiplying by the part’s CFrame