OOP Connection not disconnecting

I’m working on a game that uses OOP, and these functions refuse to Disconnect.
My actual code has a lot more, but the issue is the same nonetheless. If it helps, the way Objects are set up is that the script is required, then the .New function is called, and the resulting object is put into a Global table. When my game needs to change levels it runs a Destroy() command on the modules, this works. so far everything seems right so I’m genuinely confused why it just doesn’t disconnect.

local UIS = game:GetService("UserInputService")

local Object = {}
Object.__index = Object

function Object.new(cFrame) 
	local newObject = {}
	setmetatable(newObject, Object)
	newObject.InputBegan = UIS.InputBegan:Connect(function(k, gameProcessedEvent)
	end)
	newObject.InputEnded = UIS.InputEnded:Connect(function(k, gameProcessedEvent)
	end)                                               
	
	return newObject
end

function Object:Update(dt)

end

function Object:Destroy()
	self.InputBegan:Disconnect()
	self.InputEnded:Disconnect()
end

return Object
1 Like

Well by the looks of it, it seems as if it should be working. I’d try the following steps to make sure you are not missing anything;

  • On the ‘Destroy()’ method, have a print statement as such, ‘print(typeof(self.InputBegan))’. This will ensure that the Value ‘InputBegan’ is actually a ‘RBXScriptConnection’ and if the method is running in the first place.
  • Use a maid library such as Trove. The sole purpose of these kind of modules are to disconnect existing connections to prevent memory leaks. It’s relatively simple to implement and can save you a lot of time down the line.

Aside from that, absolutely make sure that no other methods are overwriting it or something along those lines. Good luck!

I checked using a typeof print statement and yeah it is running, I think I’ll try using Trove, thank you!

1 Like

Found my issue, there was an important variable not part of the Object.new(). Thank you very much!

1 Like

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