I have a table function new which creates a table.

local stunned = {pool = {}}
stunned.new = function(player, target)
	local self = {}

When new is called upon, it assigns these variables in the same scope as the code shown above:

local walkspeed_connector = humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
	self.old.WalkSpeed = humanoid.WalkSpeed
		
	humanoid.WalkSpeed = 0
end)	
local jumppower_connector = humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function()
	self.old.JumpPower = humanoid.JumpPower
		
	humanoid.JumpPower = 0
end)

In the same scope, a function is created inside the table named remove. This is suppose to clean up by disconnect those signals and set the table to nil.

function self:remove()
	walkspeed_connector:Disconnect()
	jumppower_connector:Disconnect()
		
	humanoid.WalkSpeed = self.old.WalkSpeed
	humanoid.JumpPower = self.old.JumpPower
		
	self = nil
end

However, when function remove is called, those events are never disconnected. The two events STILL fire against my will. walkspeed_connector and jumppower_connector are in the same scope of the new function. Am I doing something very wrong? I could really use some help. Thank you! Here is the full code:

local stunned = {pool = {}}
stunned.new = function(player, target)
	local self = {}
	
	local humanoid = target:FindFirstChildOfClass("Humanoid")
	if not(humanoid) then
		return
	end
	
	self.old = {
		WalkSpeed = humanoid.WalkSpeed,
		JumpPower = humanoid.JumpPower
	}
	
	self.player = player
	self.target = target
	
	humanoid.WalkSpeed = 0
	humanoid.JumpPower = 0
	
	local walkspeed_listener = humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
		self.old.WalkSpeed = humanoid.WalkSpeed
		
		print("WalkSpeed", self.old.WalkSpeed)
		
		humanoid.WalkSpeed = 0
	end)
	local jumppower_listener = humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function()
		self.old.JumpPower = humanoid.JumpPower
		
		print("JumpPower", self.old.WalkSpeed)
		
		humanoid.JumpPower = 0
	end)
	function self:remove()
		walkspeed_listener:Disconnect()
		jumppower_listener:Disconnect()
		
		humanoid.WalkSpeed = self.old.WalkSpeed
		humanoid.JumpPower = self.old.JumpPower
		
		self = nil
	end
	
	stunned.pool[#stunned.pool + 1] = self
	
	return self
end

6 comments

0

Why do you set self to nil? Also mind posting full code all in one code snippet? Makes it less confusing as to what goes where

0

I'm setting self to nil to remove that table from my pool of tables. Yeah, sure I'll update the thread

0

You're only removing one reference to self. Remove it from the pool table directly. The table still contains a reference to self. Also are you sure you called :remove?.

0

I removed line 42 and set the table in pool to nil after calling remove on it. Events are still firing

0

Not sure what's going on then, if you've created an instance of stunned, but I'd write it like so: https://pastebin.com/kaCu5tmH

0

Ohh wow I see what you did there.. clever. You can answer with that, and can you include an explanation on how that metamethod is working in that case? Can make a guess and say that index's made in Stunned are forwarded to the metatable in order to call Remove for each table, but a proper explanation would be appreciated. Thanks!