What would be the best way to connect and disconnect functions?

So, I have a question. I am working on the framework of my game, and I realized I have some remote events that I need to disconnect for some time. However, I also need to be able to connect these remote events again after the client tells it to. How would it be best to go about doing this? I’ve looked around the dev forum, but I still don’t fully understand the business of disconnecting and connecting. Thanks! Any and all help is appreciated!

You would need to make a variable for the connection which can be referenced later to disconnect. This variable would probably be closer to the top of the script or function so that it could be referenced by other functions.

local connection

connection = TextButton.Activated:Connect(function()
     print("hello world") 
     connection:Disconnect() -- Use Disconnect on the connection variable
end)

This should make a text button only print hello world once and do nothing more.

3 Likes

Ok. I’ve seen this done before. But how would I reconnect the function?

2 Likes

You would do the same thing again.

connection = TextButton.Activated:Connect()

1 Like

I think I get it. Thank you! (30 )

1 Like