Question on Connecting, Disconnecting events

I was working on the client side, making a UI, the ScreenGui had ResetOnSpawn set to false. My local script was located inside the ScreenGui. I had a InputBegan event in the script as well as a button that destroys the ScreenGui.

What happened was when the ScreenGui was destroyed, every time I pressed a button, I got an error.

Does this mean that I have to disconnect all the events related to the object every time I destroy something?

If a ScreenGui had ResetOnSpawn set to true, does this mean it’s creating more events that’s not being disconnected?

Also could events be stored as a value in an ObjectValue? Can another script disconnect a different script’s connection?

From what I know, script connections aren’t destroyed when the Script is destroyed.
Instead set Disabled to true, this’ll disconnect all connections.

You are probably erroring beacuse you are trying to destroy something that has been already destroyed.
ResetOnSpawn in ScreenGui means properties of children wont reset to the original state after the player resets. You should disconnect the event or check if the ScreenGui exists.

This button that destroys the GUI, is it a part of the GUI you are deleting or is it separate? If so the error might be caused by trying to call Destroy() on something that has already been Destroy()'ed.

Events are disconnected whenever the script listening for them or the Instance they are associated with are destroyed.

Nothing is destroyed yet, ResetOnSpawn is set to false and the button is inside of the ScreenGui.

I want to remove the ScreenGui completely.

What is the exact error you are getting when you click the button? Post the code as well.

The error does not come from the button, the issue is the connection is never disconnected. The error doesn’t matter since the issue is that the connection still exists which shouldn’t.

We can’t see what’s wrong if we don’t know what is causing the problem.

UserInputService.InputBegan:Connect(function(Input, GPE)
if GPE then return end
if script.Parent.Properties:FindFirstChild("Hello") then
script.Parent.Properties.Hello.Value = "Hi"
Properties is not a valid member of ScreenGui`

Again it doesn’t matter since I’m talking about the connection but here you go.

Everything here is dependent on your circumstances. Typically you should be disconnecting events you no longer need in any case rather than leave it to be done automatically, especially if you’re unsure about your scenario.

I’m not sure where your script is in relation to the Gui but if the script is under the ScreenGui and you destroy it, the connections should be disappearing as well because Destroy is recursively called on all children. Your event not disconnecting on destruction of the ScreenGui suggests otherwise but I can’t really assume that, it’s not clear in the thread where the location of these items in relation to each other is.

If a ScreenGui has ResetOnSpawn enabled and the script is under the ScreenGui, it does not create more events. Destroy is called for all PlayerGuis that have the property enabled on CharacterAdded. Furthermore, remember that connect returns a special object. If your script is destroyed, this object loses reference unless you hold on to it, thus it disconnects and gets collected.

I do not believe you can use ObjectValues to hold RBXScriptConnections as they are special objects without instances that can be tied to the DataModel, but you can pass them around by reference and they can be disconnected by other threads.

1 Like

Thanks, I think it’s because the connection is related to UserInputService and it’s not tied to any objects (because when an object is destroyed all connections tied to it will be also destroyed). But in this case UserInputService isn’t connected to a instance that is destroyed.