I was just wondering, when using connections with things such as MouseButton1Up:Connect(), RemoteEvent.On___Event:Connect(), Signal:Connect(), and a few others, when is it best to use the Disconnect functions on these? What are the times that you should and should not disconnect connections? What is the best way to setup these connections? I am just curious about some good practices to use. Thanks!
I don’t really know about signals but maybe to stop exploiters or other scripts from spamming them?
Correct me if I’m wrong though.
Usually you want to Disconnect a function when you no longer need to listen for it anymore, say if I wanted to check if a IntValue
was equal to 5 or more:
local TargetNumber = 5
local IntValue = workspace.IntValue
local Connection
local function ChangeValue(CurrentValue)
if CurrentValue >= TargetNumber then
print("Stopped at 5!")
Connection:Disconnect() --Since our TargetNumber is greater than/equal to 5, we can stop listening for this event
else
print("Number:", CurrentValue)
end
end
Connection = IntValue.Changed:Connect(ChangeValue)
while true do
IntValue.Value += 1
wait(1)
end
After we reach 5, the loop will still continue but the local function ChangeValue
won’t print anything cause after >= 5, we Disconnect the function to prevent it from continuing onwards
You can do this with scoping as well I believe, so you can have multiple Connections every time whenever a global Event is fired:
local Tool = script.Parent
local function Activated()
local Connection -- Create a Connection for every time the Tool is activated
local function DamageTarget(Hit)
if Hit.Parent then
local TargetHum = Hit.Parent:FindFirstChild("Humanoid")
local TargetTorso = Hit.Parent:FindFirstChild("Torso")
if TargetHum and TargetTorso and TargetHum > 0 then
Connection:Disconnect() -- Bingo, that's when we want to stop listening for the Touched event after finding a Target
TargetHum:TakeDamage(25)
end
end
end
Connection = Tool.Handle.Touched:Connect(DamageTarget)
wait(1)
Connection:Disconnect() -- Say no one hits the Handle, then we can just Disconnect it automatically!
end
Tool.Activated:Connect(Activated)
As a general rule you do not really need to disconnect connections, unless you specifically want some functionality to cease (permanently or temporarily, until you set up the connection again)
Bear in mind that connection to the destroyed objects will automatically disconnect, upon the object destruction.
Example Touched
or Changed
from parts.
You also do not need to worry about any local connections when the player leaves the game, since his/her client shuts down anyway.
There is some benefit to using Disconnect()
on player input events, for example different actions being taken upon click in different parts of the game. But for that purpose ContextActionService
is much better anyway.
That being said, using Disconnect()
could improve performance if done wisely. Especially connections to RunService
are very expensive and should be disconnected when no longer needed.
Thanks for the helpful response. I guess that I just didn’t really understand what the disconnect function did, so this was super helpful.