How do I make rotation relative to the raycast normal?

I am making a prop placer, the issue is that I can’t figure out a way to make the rotation work when when it’s adjusting to the raycast normal when it’s on a wall.

local function onRenderStepped()
	-- configure raycast params
	raycastParameters.FilterDescendantsInstances = { selectedProp, player.Character }
	
	-- shoot raycast
	local mouseRay = camera:ScreenPointToRay(mouse.X, mouse.Y, 0)
	local raycastResults = game.Workspace:Raycast(mouseRay.Origin, mouseRay.Direction * RAYCAST_DISTANCE, raycastParameters)
	local mousePos -- to be used
	
	-- check if mouse is in-range, and if not move it to the closet spot possible
	if not raycastResults then
		mousePos = mouseRay.Origin + (mouseRay.Direction * RAYCAST_DISTANCE)
	else
		mousePos = raycastResults.Position
	end
		
	-- If the prop is not paused
	if not pausedProp then
		-- Offset prop's position to account for it sinking half way down
		local propHeightOffset = selectedProp.PrimaryPart.Size.Y / 2 -- the part is halfway down, so it just needs to go halfway back up
																	 -- it's just not visible because we're using a BASE instead of the whole prop itself.
		if xOnlyRotation ~= nil then
			propCFrame = CFrame.new(mousePos + Vector3.new(0, propHeightOffset, 0)) * xOnlyRotation.Rotation
			if raycastResults then propCFrame = CFrame.lookAt(propCFrame.Position, propCFrame.Position + raycastResults.Normal) * CFrame.Angles(math.rad(-90), 0, 0) * xOnlyRotation.Rotation end
		else
			propCFrame = CFrame.new(mousePos + Vector3.new(0, propHeightOffset, 0))
			if raycastResults then propCFrame = CFrame.lookAt(propCFrame.Position, propCFrame.Position + raycastResults.Normal) * CFrame.Angles(math.rad(-90), 0, 0) end
		end
	end
	
	-- if rotation is activated
	if rotatingProp then
		local currentPos = selectedProp.PrimaryPart.Position

		OnlyRotation = CFrame.new(currentPos, Vector3.new(mousePos.X-.00001, currentPos.Y, mousePos.Z-.00001))
		local newPivot = CFrame.new(currentPos) * xOnlyRotation.Rotation -- Keeps position intact but applies rotation
		--local newPivot = CFrame.lookAt(currentPos, Vector3.new(mousePos.X-.00001, currentPos.Y, mousePos.Z-.00001))
		
		selectedProp:PivotTo(newPivot)
		propCFrame = newPivot
	end
	
	-- finalize prop visual
	if propCFrame then selectedProp:PivotTo(propCFrame) end
	selectedProp.Parent = game.Workspace
end

This is what happens:

This is from another game, which is what I want:

You need to rotate the object, i see that the video from the other game, the bed one, if you had placed it in the ground, it would be normal, but when you place it on the wall, it rotates

Do the same on your script, you can do this by using a simple raycast at the mousepos, and changing the direction to see if the object is at the ground or at the wall.

How would I do this? I’m confused.
Could you provide a small sample or something?