How would i make an “aiming system”

Hello so im trying to make an aiming system (u rightclick and then u just get a scope infront of ur face)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gun = script.Parent
local Camera = workspace.CurrentCamera
local empty = ReplicatedStorage.clip_empty
local reload = ReplicatedStorage.Reload
local GunShot = ReplicatedStorage["Gun shot"]
local userinputservice = game:GetService("UserInputService")

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')

local shooting = false
local equipped = false
local reloading = false

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mouseConnection

local cooldown = 0.3 -- Adjust the cooldown time as needed

gun.Equipped:Connect(function()
	equipped = true

	mouseConnection = mouse.Button1Down:Connect(function()
		if not shooting and not reloading and gun.Ammo.Value > 0 then
			shooting = true
			remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
			GunShot:Play()
			gun.Flash.BillboardGui.Enabled = true
			Camera.CFrame = Camera.CFrame * CFrame.Angles(0.23, math.rad(1), 7)
			gun.Ammo.Value = gun.Ammo.Value - 1
			wait(.2)
			gun.Flash.BillboardGui.Enabled = false
			script.Enabled = false
			remoteEvent.Parent = game.Workspace
			wait(cooldown)
			script.Enabled = true
			remoteEvent.Parent = game.ReplicatedStorage
			shooting = false
		elseif gun.Ammo.Value == 0 then
			empty:Play()
		end
	end)
end)

userinputservice.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.R and not reloading then
				reload:Play()
				reload.Ended:Wait()
				reloading = true
				wait(cooldown) -- Add a cooldown before allowing shooting again after reloading
				gun.Ammo.Value = 15
				gun.Magazine.Value = gun.Magazine.Value - gun.Ammo.Value
				reloading = false
			end
		end
	end
end)

gun.Equipped:Connect(function()

userinputservice.InputBegan:Connect(function(input2, GameProccesed2)
	if not GameProccesed2 and equipped == true then
		if input2.UserInputType == Enum.UserInputType.MouseButton2 then
				print("Clicked")
				game:GetService("Workspace").CurrentCamera.CameraType = Enum.CameraType.Scriptable
				while wait(.0000001) do
					game:GetService("Workspace").CurrentCamera.CFrame = gun.Flash.CFrame
					end
		end
	end
end)

	gun.Unequipped:Connect(function()
		game:GetService("Workspace").CurrentCamera.CameraType = Enum.CameraType.Custom
	equipped = false
		mouseConnection:Disconnect()
	end)
end)

it kinda works but uhhh, first thing: when i rightclick i just start spinning, second thing: when i unequip the gun my camera just gets stuck and i keep spinning after equipping the gun.