Will Changing ObjectValue.Value Cause a Memory Leak If I Don't Disconnect The Function?

Hello Developers!

I’m working on a selection system for Parts in the workspace and I need to store a refence to the part with ObjectValue. So, I detect ObjectValue.Value.Changed and connect that to a function. Like this code here :arrow_down:

script.ConnectedObject.Value.Changed:Connect(function(Property: string)
	-- Do Stuff
end)

Now, let’s say I change ObjectValue.Value to a different Instance in the workspace.

Do I have to Disconnect() the ObjectValue.Value.Changed to prevent a memory leak? I asked Roblox’s AI assistant. (And check the official documentation) Here’s what I got from it:

“According to the Roblox documentation, connecting a .Changed event to an ObjectValue.Value and then changing said ObjectValue.Value with the .Changed event still connected will not cause a memory leak.” - :robot:

I wanted to run this by developers who know more about memory leaks and all that stuff.

Yes, you have to disconnect it if you no longer wish to listen for changes, and aren’t destroying whatever connectedObject.Value is (things get a little fuzzy when it comes to nil instances but to be safe, it’s a good idea to destroy the instances as a safeguard).

Further, changing the objectvalue’s value won’t “update” the changed connection, the event will still fire/the connection’s func will still run for the instance that is the connectedObject’s value at the time of creating the connection

1 Like

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