Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you disable Button1Down:connect(function()?

I am trying to figure out how to disable this with a key bind or a toggle on a GUI

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Mouse.Button1Down:connect(function()
   for i=1, 100 do
-- then some more code after
end
end)

1 answer

✓ Accepted answer

You could use a simple debounce, however, the given function would still be running everytime Button1Down fires. Thus, it would be more optimal to use the :Disconnect() method, the reverse of :Connect(). See the example below for details:

local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Connection

local function MouseClicked()
    print("Clicked!")
end

Connection = Mouse.Button1Down:Connect(MouseClicked)

UIS.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.T then --or whatever key
        Connection:Disconnect()
    end
end)

Now, I can click as many times as I want, and it will continue to print Clicked. This works, until, of course, I press the T key. Then, the Button1Down event is disconnected from the MouseClicked function, and it will no longer run.

As always, comment with any questions or concerns.


##Resources

Event Connections

This discussion is preserved as it was. New answers, comments, and votes are closed.