Need help with connecting and disconnecting functions

I am a little confused on connecting and disconnecting functions. I am currently working on optimizing the code for my first game, and I have run into a problem involving connections. I read on the dev hub that you should disconnect your functions when they are not in use, but I’m not entirely sure how to do this. Here is my code for a GUI that changes textcolor when the player hovers over it.
I am working on optimizing my code for my first game. Here is the code for hovering over a GUI. local Buttons = {
[“UpdateLogButton”] = HomeScreen.Buttons:WaitForChild(“UpdateLogButton”);
[“EndingsButton”] = HomeScreen.Buttons:WaitForChild(“EndingsButton”);
[“PlayButton”] = HomeScreen.Buttons:WaitForChild(“PlayButton”);
}

local function GuiHovers()
	
	for i, button in pairs(Buttons) do
		
		local connection
		local connection2			
		
		connection = button.MouseEnter:Connect(function(x, y, button)
			
			print("Hovered")
			
			connection:Disconnect()
			
		end)
	
		connection2 = button.MouseLeave:Connect(function(x, y, button)
			
			print("Left")
			
			connection2:Disconnect()
			
		end)
	
	end
	
end

``
As expected, the script prints "Hovered, and “Left” one time, and then stops. I want to reconnect it every time the player hovers over it again, but where could I establish the connection again? Any and all feedback is appreciated!

I don’t think connecting/disconnecting functions are necessary if you’re just wanting to detect when the Mouse hovers or leaves over a Button object? You should only disconnect them if you want them to fire only once

You are able to use them again though by calling the function but again, they’ll only fire once & disconnect

2 Likes

Ok. This makes sense. I read the following from the Dev Forum and got a little confused. " Always call Disconnect() when a connection is no longer needed. Forgetting to do so can cause your game to use more resources than necessary. Note, however, that this may not always be necessary; when an object is destroyed, all connections to that object’s events are disconnected automatically."

However, one more question if you don’t mind:
When should I disconnect events? When should I not? I guess I’m still a little confused to as in what cases I should use disconnect. I don’t want connections to overlap on each other.

Pretty much when you don’t need them anymore

Say I have a local function, a Connection variable, and a Changed Event for a NumberValue that will fire each time the NumberValue is changed:

local Value = workspace.NumberValue
local Connection

local function CheckChange(NewValue)
    if NewValue >= 50 then
        Connection:Disconnect() --We'll stop listening for any more changes here if the New Value is greater than/equal to 50
    end
end

Connection = Value.Changed:Connect(CheckChange)

We’re setting our local Connection at the start of the script so that we’re able to access it from anywhere, and we’re setting it to detect any “changes” via using the Changed Event

If the new value is greater than or equal to 50, we can disconnect that specific function since we don’t need it anymore, preventing it from calling any more changed made by the event

Analogy: Think of it as like throwing away a soda can after you’ve finished drinking all of its liquid; you don’t need it anymore so you can throw it away

2 Likes

Alright, this making more sense now. Thank you!

1 Like