:Disconnect() Clarification (Disconnecting events)

Hi,
I first want to say I am a very new scripter and am currently creating a simple automated minigame to get well-rounded in the fundamentals. I was having an issue where an event would multiply every single time a new round would start. I figured out through the devforum that :Disconnect() will stop the event from firing multiple times. I also saw programmers saying on here the purpose of :Disconnect() is when you only want something to happen for a limited amount of time and then you’re done with it. However, in this case, the functions run every single time a new round starts.

I guess in simple terms I don’t understand why :Disconnect() fixed my problem? (I hope this made sense)

Because without the usage of Event:Disconnect(), you are telling the code to create a new Event everytime which will add up overtime, so when a specific condition occurrs, it will fire the Events that are Listening, which would be multiple.

1 Like

So essentially in this case since the code is rerunning by itself the events that ran in the previous minigame are still running in the new one?
And are there specific cases where you wouldn’t necessarily need to disconnect an event? Like does this only apply to remote events? Because I’ve never had a problem with code repeating with for example a player dying.

Essentially, The Previous Events are still Intact when you repeat the cycle, which is why you use Disconnect to get rid of them to make way for the new ones.

It applies to all Events, If you create multiple Connections for the same Event, you will have multiple Connections for the same event.

If you make 2, it will fire 2 times.

But in the case of a Player dying, when they die, their character is Respawned, which should automatically remove the connection as it has nothing to listen to.

When you say ‘make 2’ are you meaning 2 remote events?

No. I mean this:

RemoteEvent.OnServerEvent:Connect(function)
RemoteEvent.OnServerEvent:Connect(function)

You would basically be making 2 connections, which would fire 2 times if you call them. Since there are two connections listening to be called for the same RemoteEvent.

Well I didn’t make multiple events for the same thing, or are you saying that’s what it would look like the second time it ran without disconnecting it?

Thats basically what would be happening if you dont disconnect them for keep track of them when restarting the game.

You are likely under the impression that a new connection replaces the previous. Connections under-the-hood are appended, in other words multiple connections can exist per signal; automatically deallocated from memory only after they’re no longer reachable by Lua + disconnected.*Calling :Destroy() on an instance disconnects any/all connections.

1 Like

in short, RBXEventConnection:Disconnect() is to kill the listening session. so you won’t get any more data from the event.

for an example

local Listen = remoteevent.OnServerEvent:Connect(print) -- the listener is the "print" function. it will print any data received from the "OnServerEvent" event.
-- waits for 4 seconds
task.wait(4)
-- disconnects the event.
Listen:Disconnect()
-- now the "print" function above will not be printing anything, even if you tried to fire 100 datas thru the remoteevent.
2 Likes

I can definitely help explain why using :Disconnect() fixed your issue!

In general, when you create an event listener in Roblox using the :Connect() method, the event listener will continue to listen for the event until the script is stopped or the listener is disconnected. This means that if you call :Connect() multiple times, you’ll end up with multiple listeners that will all fire when the event is triggered.

In your case, it sounds like you had an event listener that was being created multiple times, but wasn’t being disconnected when the round ended. This means that each time a new round started, a new event listener was being created and added to the previous ones that were still active. As a result, the event was firing multiple times and causing issues with your game.

By using :Disconnect(), you’re essentially telling the event listener to stop listening for the event and to disconnect itself from the event. This means that the event listener will no longer fire when the event is triggered, and you won’t end up with multiple listeners that are all firing at the same time.

It’s worth noting that using :Disconnect() is not always necessary or appropriate, but in your case, it was the right solution to fix the issue you were experiencing. I hope this helps clarify things for you!

1 Like

Oh okay I see what you’re saying.
Also in this case I noticed if I disconnected events within a local script it would cause the script to stop immediately. I’m having trouble explaining so hopefully I can just show you.
Server

local connection
connection = remote.OnServerEvent:Connect(function(plr, msg)
	if msg == "Round Over" then
		connection:Disconnect()
		print("The round is over.")
		remote:FireAllClients("Winners", minigameModule.winners)
               end
         end)

Local Script

remote.OnClientEvent:Connect(function(strng, value)
	if strng == "Winners" then
		TmrFrm.Visible = true
		for i = settngs.Intermission,0,-1 do
			TmrTxt.Text = i
			wait(1)
		end
		TmrFrm.Visible = false
end)

In the local script I don’t disconnect because if I do it breaks. I don’t know why that is

You didn’t even try to hide the fact you plagiarized off ChatGPT.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.