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