Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
1
Answered by 4 years ago

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

Ad

Answer this question