What do you want to achieve? Keep it simple and clear!
I need help using Disconnect() so that it doesn’t make my scripts work only once and never again.
What is the issue? Include screenshots / videos if possible!
I have a lot of scripts in my game that use :connect(function()). After moving through my game, eventually these just stopped working. I figured it was due to memory leak from tens of open connections, so I added disconnect() to them. Now they only work once. Which is an issue. I don’t know what to do. My code is broken with disconnect and without it.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Here is my code for a door:
local connection
connection = script.Parent.MouseClick:Connect(function(playerthatClicked)
local remoteEvent = game.ReplicatedStorage:WaitForChild("doorcutscene3")
remoteEvent:FireClient(playerthatClicked)
playerthatClicked.PlayerGui.Inventory.Enabled = false
wait(3.6)
playerthatClicked.PlayerGui.Inventory.Enabled = true
playerthatClicked.Character:MoveTo(game.Workspace.bathroomdoorbeacon1.Position)
connection:Disconnect()
end)
As I said, it works but only once. What should I do? I don’t think completely removing disconnect() is an option as I said earlier.
What Disconnect does is it makes it so that from that moment on, the function will not fire. The reason it executes only once is once it fires, the function disconnects itself. Thus, it can no longer execute.
There isn’t going to be a memory leak if the connection is only connected once. So if you want your doors to work forever you shouldn’t disconnect the events.
If you just want it to only fire whenever a specific Player clicks it, why not just create a table of Players that have already pressed the ClickDetector?
local ClickDetector = script.Parent
local RemoteEvent = game.ReplicatedStorage:WaitForChild("doorcutscene3")
local ClickedPlayers = {}
local function Clicked(Plr)
local PlrGui = Plr:WaitForChild("PlayerGui")
local Chr = Plr.Character
if not table.find(ClickedPlayers, Plr) and Chr then
table.insert(ClickedPlayers, Plr)
RemoteEvent:FireClient(Plr)
PlrGui.Inventory.Enabled = false
wait(3.6)
PlrGui.Inventory.Enabled = true
table.remove(ClickedPlayers, table.find(ClickedPlayers, Plr))
end
end
It won’t cause any issues as long as you are using them correctly. For example: You should only connect the door events ONLY ONCE. If you need to connect a door connection more than once then you should disconnect the previous connection for that door. BUT as long as you use :Connect() once per door than you shouldnt have an issue.
:Disconnect() is used in cases when there’s a function in a function
tool.Equipped:Connect(function()
local connection = script.Parent.MouseClick:Connect(function()
print("A")
if connection then connection:Disconnect() end
end)
end)
Hm. I’ll take that into consideration and edit my code appropriately. Although before I added disconnections, there was still something making my clickdetectors stop detecting. If it wasn’t memory leakage than I don’t know what. I’ll come back in a while if that issue reappears.