How do I get rid of runService.Heartbeat in OOP

I want to disconnect the heartbeat event from the previous object when I create a new object. When I create a new object, I set the old object to nil

The heartbeat event is inside of a separate module script and is called inside of a method

--Heartbeat event 
function contents.stinkPassive(self) 
    local Player = self.Player 
    local Char = Player.Character or Player.CharacterAdded:Wait()
    local HMR = Char:WaitForChild("HumanoidRootPart")

    runService.Heartbeat:Connect(function() -- Keeps printing out hi even after I have set the object to nil 
        print("hi?") -- How do I disconnect this?
    end)
end

if self.passiveEffect then -- This is inside the method
    self.passiveEffect(self) -- This runs stinkPassive()
end 


Not too sure what you are trying to do but will this do?

function()
  _G.a=hb:Connect(function()end)
end

In another script:

_G.a:Disconnect()

This article might help you, since you may have to use Disconnect() for that issue, which disconnects that function.

Example of Disconnect() being used:

local points = Instance.new("NumberValue")
points.Name = "Points"
points.Value = 0
 
local connection
 
local function onPointsChanged(newPoints)
	print("Points: " .. newPoints)
	if newPoints >= 50 then
		-- Stop listening for changes if we have at least 50 points
		connection:Disconnect()
	end
end
 
connection = points.Changed:Connect(onPointsChanged)
 
-- Trigger some changes
points.Value = 25
points.Value = 100
points.Value = 0  -- Prints nothing because we called Disconnect()

Whenever I equip a new tool to the backpack, I create a weapon object for it, that weapon class has a method that gives a damage effect to it. Inside that same method I call a function from a different module script and inside the called function contains a runService heartbeat event and it won’t stop. I want to find a way to stop that runService heartbeat event when I set the object to nil.

I know what disconnect() does, I need a way to know when my object is set to nil. When I set the object to nil currently it doesn’t garbage collect the runService event

Instance.AncestryChanged fires when the ancestry (parent, parent of it, and so on) of an instance changes, for example, when it is destroyed. You can disconnect the heart beat event when it happens.

You can store the heartbeat connection inside the object itself.

self.heartbeat = runService.Heartbeat:Connect(function() -- Keeps printing out hi even after I have set the object to nil 
        print("hi?") -- How do I disconnect this?
    end)

Wherever you set the object to nil.

self.heartbeat:Disconnect()
1 Like

Does this work with custom objects?

Can this be done inside of a module script where I can require it?

@DreamWakeStudios made his post while I was writing this

Link the function as a property of table “self”.

Think of it like any other variable of a script connection:

self.Heartbeat = RunService.Heartbeat:Connect(function()
    print("hi?")
end)

You can then simply disconnect it when you destroy the object directly, or indirectly using a maid:

self.Heartbeat:Disconnect()

Alternatively, you can cancel the connection using an error handler in the Heartbeat function:

local connection
connection = RunService.Heartbeat:Connect(function()
    local success = pcall(function()
        -- make a notion to the object, or have a value in the self table signifying if the table is active
    end)
    if not success then 
        connection:Disconnect() 
    end
end)
1 Like

You can disconnect from anywhere you have a reference to the object itself. If you are just scanning the workplace and :Destroy() on the instance of it then you aren’t making use of the OOP object class.

Seeing as how you are referencing self.passiveEffect above, I am hoping you are keeping track of objects you create with your OOP class somewhere to utilize them everywhere. If you are, it won’t matter where you Disconnect since you will be doing so on the object itself.

I tried creating a property called self.Connection and set the value to nil

then I made self.Connection equal to the runService event

image

when I set the object to nil it still runs

Do I have to loop through all of the self properties and set them to nil manually?

I store my reference of my object into a dictionary

image

but when I do

image

the runservice is still there

Disconnect it first then set it to nil.

PillowReference[userId].Connection:Disconnect()
PillowReference[userId] = nil
2 Likes

Does the maid have something to do with this?

Yes that is correct. Maids are just as the name implies, something that handles things for you. Indirectly cleaning up connections. Otherwise, directly managing the state is what you just accomplished by typing out the Disconnect event in the specific spot the state required it to be done.

The article linked in that post sums it all up nicely.

1 Like