Need help with top-down camera system

  1. What do you want to achieve? Keep it simple and clear!

I want to make a top-down camera controllable by the player’s mouse, that you can click and drag to move around.

  1. What is the issue? Include screenshots / videos if possible!

The core system works, which is clicking and dragging, but the camera randomly stops facing down once you click and it also resets the camera’s position whenever you click again.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have attempted to find tutorials on this, but there is a big lack of tutorials in the field of top-down camera systems like I want to achieve.

Here is the code:

local plr = game.Players.LocalPlayer
local char = game.Workspace:FindFirstChild(plr.Name)
local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()
local Cam = game.Workspace.CurrentCamera
local camPlace = game.Workspace.CamPlace
Cam.CameraType = "Scriptable"
Cam.CFrame = game.Workspace.CamPlace.CFrame
local mouse = plr:GetMouse()
local moving = false
local dragSpeed = 1.1

function getMouseTarget()
	local cursorPosition = game:GetService("UserInputService"):GetMouseLocation()
	local oray = game.workspace.CurrentCamera:ViewportPointToRay(cursorPosition.x, cursorPosition.y, 0)
	return oray

-- Zoom
	Cam.CFrame = Cam.CFrame + (Cam.CFrame.ZVector * -3)

Cam.CFrame = Cam.CFrame + (Cam.CFrame.ZVector * 3)

local dragOrigin

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton3 then
		dragOrigin = game:GetService("UserInputService"):GetMouseLocation()
		moving = true
		repeat game["Run Service"].Heartbeat:Wait()
			local CPos = game:GetService("UserInputService"):GetMouseLocation()
			local difference = CPos-dragOrigin
			print(difference.X, difference.Y, difference.X * dragSpeed, difference.Y * dragSpeed)
			Cam.CFrame = * dragSpeed, Cam.CFrame.Y, difference.Y * dragSpeed)
		until moving == false

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton3 then
		moving = false

Here is a video showcasing the issue: