Hello everyone,
I have a question about how event listeners work in Roblox when resetting a character multiple times. I’m working with a script that listens to a Humanoids Health
property change using GetPropertyChangedSignal("Health")
, and I’m curious if event listeners accumulate in memory when the character resets.
Here’s the code I’m working with:
local healthBarModule = require(game:GetService("ServerScriptService").Modules.HealthBarManager)
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
-- Make Sure the Character has a Humanoid
local hum = char:FindFirstChildOfClass("Humanoid")
if not hum then
warn("no humanoid of: "..plr.Name)
return
end
-- Add HealthBar to the new Character
healthBarModule.Add(char)
print("HealthBar added")
-- If the health changes, update the Health Bar:
hum:GetPropertyChangedSignal("Health"):Connect(function()
print("hello")
healthBarModule.Update(char)
end)
end)
end)
The Question:
If a player resets their character multiple times (let’s say 10 times), does each reset cause a new GetPropertyChangedSignal("Health")
event listener to accumulate in memory? In other words, after 10 resets, would there be 10 active listeners in memory, and if the health changes, would it print “hello” 10 times?
From my understanding, the event listener should only be registered once per character, even if the character is reset. However, I’m unsure if Roblox automatically cleans up old event listeners when the character resets, and that’s why I’m asking here.
Can someone clarify whether these listeners do accumulate, or if they are properly cleaned up when the character resets?
Thanks in advance for any insights!