How to make this toggleable?

I have this lock on script, but it isnt togglable. Ive tried to make it toggleable by using booleans but it didnt work. I think it didnt work cause im using run service but idk :sweat_smile:

Heres the script

repeat task.wait(1) until game:IsLoaded() task.wait(1)

local rs = game:FindService("RunService") or game:GetService("RunService")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local camera = game.Workspace.CurrentCamera
local UIS = game:FindService("UserInputService") or game:GetService("UserInputService")
local bool = false

local char = plr.Character
local rootPart = char.HumanoidRootPart
local TS = game:FindService("TweenService") or game:GetService("TweenService")



UIS.InputBegan:Connect(function(input, chat)
	if chat then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton3 and bool == false then
		bool = true
		rs.RenderStepped:Connect(function(fps)
			camera.CameraType = Enum.CameraType.Scriptable
			mouse.Icon ="rbxassetid://8399591114"
			print("h")
			local MPos = mouse.Hit.p
			
			local info = TweenInfo.new(1)
			
			local tweem = TS:Create(camera, info, {CFrame = CFrame.new(camera.CFrame.Position, MPos)})
			tweem:Play()
			
			camera.CameraType = Enum.CameraType.Custom
		end)
	end
end)

Help is appreciated, thank you for your time :smiley:

Check if you have a connection, and either disconnect it (if you already have one) or make a new one (if you don’t).

Something like

-- this will be nil when we're not active, and
-- a RbxScriptSignal when it is active
local connection = nil

UIS.InputBegan:Connect(function(input, gameProcessed)
  if gameProcessed then return end

  if input.UserInputType == Enum.UserInputType.MouseButton3 then
    if connection then
      -- we already have one
      connection:Disconnect()
      connection = nil
    else
      -- we need a new one
      connection = rs.RenderStepped:Connect(function(fps)
        -- ...
      end)
    end
  end
end)