Will this get garbage collected?

image

I set mouse to nil

1 Like

Would need the whole script to know.

mouse is set to nil. I already set mouse to nil.

The tool is parented to nil.

What would’ve been a better difference is disconnecting the button1down and button1up when the tool is nil, though if the tool is nil then the whole script wouldn’t exist? assuming the script is parented to the toool.

why are you guy so bad at answering questions. I know what i’m doing.

I was wondering if setting mouse to nil would automatically disconnect the connections.

If you knew what you’re doing, if anyone knew what they were doing, no questions would be asked. Good answers require good context. Your question was not clear at all, do not get mad at people who have given up their own time to try and help your issue when you barely give any of your time to compose a proper question.

3 Likes

You’d ideally want to save the connection to a variable and then use

RbxScriptConnection:Disconnect() on it when you don’t need it anymore

The events still apply, its just that the mouse wont be referenced in the code again until its is redefined.

Garbage collection is removing unused data(AKA garbage data)
which is why it got its name.
The mouse may not be used in the code but it is still being used in other stuff.
Roblox Objects are not garbage collected.
which is why stuff like RBXEvents, RBXScriptSignals, and Instances are never garbage collected, but have to be removed.

A great way is to disconnect then is to Use :Disconnect()

1 Like

Example

local mouseConnection

mouseConnection = mouse.Button1Down:Connect(function()
--code here
end)


--and when you need to disconnect

mouseConnection:Disconnect()

oh i see. That makes sense. So I disconnect it. I thought roblox automatically checks for any nil references in connections and removes those but it seems that only works on instances.

my question was simple.

Mouse.Button1Down:Connect(function() etc etc end)

wait(3)

Mouse = nil

Because the reference to the connection is Nil I would assume it gets disconnected from the script cause now the script will wait for a signal for:
nil.Button1Down:Connect()

You would have to know if there are other references to the Mouse object, there probably are, outside your script.

Dude this is not how you are gonna get answers here. Really, it’s your fault that you aren’t getting what you need because you did not provide the info that people need to know to answer your question fully.

Don’t understand “why are you guy so bad at answering questions. I know what I’m doing.”

If you understand what you are doing, feel free to not ask questions and expect people to answer them for you when you talk like this.

People are trying to help others, and instead you want to sit here and act like it’s their fault for explaining something you want to hear exactly the way you want it explained.

Dk why everyone isn’t directly answering bro’s question but instead providing their own solutions for some reason, and then getting mad at him because he didn’t provide the so called “context”.

Anyways, enough ranting, back to the question now. If the reference of an object is set to nil, then it will automatically get garbage collected. However, the connections of that instance won’t be cleaned up along with it. As such, I would advise you to :Destroy instances when they are no longer needed as it sets the Parent to nil and disconnects all connections. This won’t clean up the object instantly but since the object has no references, it will get garbage collected eventually.

1 Like