My doubt when disconnecting events from functions

I’ve been studying disconnecting events on functions, but I don’t quite understand it.
Well, my question is, for example, when making a Part.Touched function, when something touches the part, ¿does it creates a new connection every touch? ¿or does it just create one and takes the same connection after touching again? ¿is necessary to disconnect a Part.Touched function every time?

1 Like
 -- this is a part
local part = workspace.fgsdfgsdfgetgdfg  

-- this is a connection
local connection = part.Touched:Connect(function()
	print('asfasdfasdgf')
end)

--[[
this connection will run the function each time something touches this part, not create a new connection as that would be a memory leak
when you want it to stop running the function, disconnect it.
]]

connection:Disconnect()

--now, the function does not run whenever something touches the part and it returns to being a normal, non special part.
--unless if you connect .Touched with another function

If you wanted a connection to run only once then you would use :Once() instead of :Connect()
Still try to disconnect it to prevent memory leaks when it is not needed anymore.

2 Likes

so,

local connection = part.Touched:Connect(function()
print(‘asfasdfasdgf’)
end)

does not cause memory leak and

part.Touched:Connect(function()
print(‘asfasdfasdgf’)
end)

does?

1 Like

since the second example isnt referenced then yes there would be no way to disconnect it except for :Destroy()'ing the part. the first one can be disconnected later on by calling connection:Disconnect()

Everytime you do event:Connect() You’re making a connection, so, every time the part is touched, a new connection is created, right? so, my question is, how do i disconnect every of all these connections instantly after script runs?will local connection = part.Touched:Connect(function() make one connection for every touch?

No. if you included a new part.Touched:Connect() WITHIN the connection function then yes, you would be creating a new connection each time which is bad.

1 Like

When you create an event, for instance, the Touched function applied to an object, it makes a new connection every time something or someone touches the part (hit). A new connection is established each time it has been touched.

Regarding the necessity to disconnect it, it depends on your use of it. If you want to make that so the Event is disconnected when someone touches the part, then using disconnect is a prudent option to do so. If you need to run the Event once, you can either try using :Once or disconnecting it when it has been touched. The difference between :Once and :Disconnect is that the function must be triggered once when it is executed the first time, while :Disconnect is like :Once, but you can disconnect the function under specific conditions.

To disconnect a function, you must write the connection (:Connect) as a variable, and then disconnect it:

local function OnTouch(a,b,c)
return a * b * c
end

local connection = part.Touched:Connect(OnTouch)

connection:Disconnect()

As @StingyDudeman65 said when you include part.Touched:Connect() in a Connect function, you’d be creating a new connection all time. That’s why you should store it on a variable so you would be storing the function/connection

local p = Instance.new('Part')

-- GOOD! No memory leaks
local ev = p.Touched:Connect(function()
	print('HI')
end)
ev:Disconnect()

--BAD! MEMORY LEAK
p.Touched:Connect(function()
	p.Touched:Connect(function()
		
	end)
end)


--BAD! WILL NEVER BE DISCONNECTED UNLESS PART IS REMOVED!
p.Touched:Connect(function()
	print('HI')
end)


-- IDK. WILL ONLY FIRE ONCE, MAY STILL NEED TO BE DISCONNECTED
local ev = p.Touched:Once(function()
	print('HI')
end)
ev:Disconnect()

i don’t understand your script, it won’t work because you are disconnecting it instantly without even running it first. I want to disconnect the event after script runs, but that when i touch the part again, a new connection will be made, keeping the other connections deleted. Storing them on a variable and then disconnecting it after won’t help me because i don’t want to stop the script from working (deleting all connections at once).

1 Like

That was just an example script. Same with the one i sent.

It was only an example script. The main thing you must understand is how :Disconnect works and when to use it.

local p = Instance.new(‘Part’)

– GOOD! No memory leaks
local ev = p.Touched:Connect(function()
print(‘HI’)
end)
ev:Disconnect()

this will disconnect the event instantly and script won’t work. Could you give me a working example please?

Again, it is a example script. It is not meant to do anything but show you a example.

I understand that, but i don’t really know where should i put that :disconnect() line, because i want it to disconnect instantly, but keeping that script would create new connections when touching, maybe this doesn’t work like i’m thinking and it’s impossible…

An example would be maybe a temporary lava pool or something. make the connection meant to harm players, and, after maybe 20 seconds or something, disconnect the connection and remove the lava pool

Disconnecting the kill function would be better than just destroying the lava because you could then do something cool with it, maybe animate it fading away rather than just making it vanish instantly

But if i want the lava pool to be in the game permanently, it is not necessary to disconnect? Because my case is, i have a game where i have gamepass pads, which work by touched events, and them should be there permanently, so each connection created does not need to be disconnected?

If the lava pit is permanent or part of a map then yes there is no need to disconnect

Okay thanks you! I think I’ve got it now