Need help with disconnecting functions

Hello,
I have a question.

What is the difference between
–when I disconnect a function within the function block
–when I disconnect a function outside the function block

local swordequippedcon

swordequippedcon = Sword.Equipped:Connect(function(
     -- do something
     swordequippedcon:Disconnect()
end)

Based on the code above, will the code still listen for the equipped event?
If it doesn’t, how do i make it so that it does, or should I just leave it alone and don’t disconnect it.

This method is used for when you want a connection to only fire once. If you want a function to fire multiple times, then you do not disconnect the connection. So you should leave it like this:

Sword.Equipped:Connect(function()
	-- Code
end)

But, if you wanted the event to only fire once for some reason, then that is when you would use the technique you wrote.

For example, you could use it for a landmine:

local doExplode
doExplode = landmine.Touched:Connect(function()
	doExplode:Disconnect()
	-- Explode
end)
1 Like
local swordConnection = nil;
local sword = script.Parent

sword.Equipped:Connect(function()
    -- Code
end)

randomProximityPrompt.Triggered:Connect(function()
    swordConnection:Disconnect()
    sword:Destroy()
end)

This is just an example code. You can use this in many different ways. I’ve also made a post a while ago. Here it is!

(Honestly I’ve learnt a lot just from there)

1 Like

Thank you very much for all the help!

1 Like