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

How does :Disconnect() work and can it be used on a disconnected event?

Asked by 5 years ago

Recently I was writing some code that would disconnect two arrow buttons if the screen was cleared. Because the clear button can be clicked again before the events are connected again, I decided to use a boolean variable canDisconnect that is set to true when the events are connected again. Here is an excerpt from my code:

local db = true
clearButton.MouseButton1Click:Connect(function()
    if db then
        db = false
        if canDisconnect then
            connection:Disconnect()
            connectionTwo:Disconnect()
        end
        clearScreen(frame)
        wait(5)
        db = true
    end
end)

One of the events that changes canDisconnect is this one:

connection = rightArrowButton.MouseButton1Click:Connect(function()
    canDisconnect = true
    if db then
        db = false
        if pages[pageNumber.Value + 1] then
            print("there is another page")
            pageNumber.Value = pageNumber.Value + 1
            loadPage(pages[pageNumber.Value], frame) 
            getIngredients.OnInvoke = function(name)
            --code                  
            end 
        end
    wait(1)
    db = true
    end
end)

It would be much better for me if I could use :Disconnect() on connection even if it has already been :Disconnected. This leads into the second part of my question. How does :Disconnect() work? I know what it does, however, I don't know how it works. Does it set connection = nil? For some reason it does not seem like that would work. I would appreciate any insight in this matter. Thanks!

1 answer

Log in to vote
2
Answered by 5 years ago

I just tested it in studio, and connections can be disconnected more than once without throwing an error. You can compare connections with plugs in a wall. You can unplug them, but they're still there. If they're unplugged, the attached device isn't being charged. Obviously in this case you can't unplug an already unplugged charger, but it worked when I tested this out in studio. Good luck!

Ad

Answer this question