Disconnecting events in another function

I know how to quit an event with :disconnect() but there’s a code:

function activeClicking()
   gui.MouseButton1Click:Connect(function()
      print("Clicked")
   end)
end

activeClicking()

I want this event to work only when the function is called. And I want to disconnect it outside of the function? Is that possible and if it’s how can I do that?

Thanks for reading.

You make a variable that is accessible anywhere in the script, then refer the variable to connect or disconnect an event.

1 Like
local connection

function activeClicking()
   connection = gui.MouseButton1Click:Connect(function()
      print("Clicked")
   end)
end

activeClicking()
connection:Disconnect()

Like this?

1 Like

Yes. That’s how you can do it.