Will this cause a data leak?

Whenever the player leaves the game will the health changed connection be destroyed?

local ReplicateStorage = game:GetService("ReplicatedStorage");
local TweenService = game:GetService("TweenService");
local CollectionService = game:GetService("CollectionService");
local RunService = game:GetService("RunService");
local Players = game:GetService("Players")
--
local Maid = require(ReplicateStorage:WaitForChild("Modules").Misc.Maid);
local Thread = require(ReplicateStorage:WaitForChild("Modules").Misc.Thread);
local Maid = require(ReplicateStorage:WaitForChild("Modules").Misc.Maid)
local RemoteHandler = require(ReplicateStorage:WaitForChild("Modules").Misc.RemoteHandler)
--
local CAMERA = game.Workspace.CurrentCamera;
--
local humanoidControl = {};
local humanoidControl_mt = {__index = humanoidControl};

local StatsOpenedUDim2 = UDim2.new(0, 0, 0.9, 0);
local StatsClosedUDim2 = UDim2.new(-0.23, 0, 0.9, 0);

function humanoidControl.new(Player)
	local self = {};
	
	self.Player = Player;
	self.Mouse = Player:GetMouse();
	self.Character = Player.Character;
	self.Humanoid = self.Character:WaitForChild("Humanoid");
	self.Hrp = self.Character:WaitForChild("HumanoidRootPart");
	self.Head = self.Character:WaitForChild("Head");
	
	self.Maid = Maid.new();
	
	if RunService:IsServer() then
		self.HealthBar = ReplicateStorage.Miscs.HealthBar:Clone();
		self.HealthBar.Parent = self.Head;
		self.HealthBar.PlayerToHideFrom = Player;

		self.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None;
		CollectionService:AddTag(self.HealthBar, "HealthBar");
	else
		self.Collection = {};
		
		Thread:Spawn(function()
			Thread:Wait(0.1);
			for _,v in pairs(CollectionService:GetTagged("HealthBar")) do
				self:AddConnection(v);
			end;
		end);
		
		self.Maid.Added = CollectionService:GetInstanceAddedSignal("HealthBar"):Connect(function(Child)
			self:AddConnection(Child);
		end);
		
		self.Maid.Removed = CollectionService:GetInstanceRemovedSignal("HealthBar"):Connect(function(Child)
			if Child then
				Child.Enabled = false;
			end;
			
			self:CleanUpConnections();
		end);
	end;

	self.Maid.Died = self.Humanoid.Died:Connect(function()
		--[[if RunService:IsServer() then
			CollectionService:RemoveTag(self.HealthBar, "HealthBar");
		else]]
		if RunService:IsClient() then
			self:CleanUpConnections();
		end;
		self.Maid:Destroy();
	end);
	
	return setmetatable(self, humanoidControl_mt);
end

function humanoidControl:CleanUpConnections()
	print("test");
	for _,v in pairs(self.Collection) do
		if v["Character"] == nil or v["Character"] and v["Character"]:FindFirstChild("Humanoid") and v["Character"].Humanoid.Health <= 0 or v["Gui"] == nil then
			if v["Gui"] then
				v["Gui"].Enabled = false;
			end;
			if v["Connection"] then
				v.Connection:Disconnect();
				print("c4 cut")
			end;
			table.remove(self.Collection, self.Collection[v]);
			v = nil;
		end;
	end;
end;

function humanoidControl:AddConnection(v)
	v:WaitForChild("Frame").Username.Text = v.Parent.Parent.Name;
	v.Enabled = true;

	if v.Parent.Parent:FindFirstChild("Humanoid") then		
		local Update
		Update = v.Parent.Parent.Humanoid.HealthChanged:Connect(function(Health)
			local Amount = Health / v.Parent.Parent.Humanoid.MaxHealth;
			
			TweenService:Create(v.Frame.Bar.Fill, TweenInfo.new(0.25, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Size = UDim2.new(Amount, 0, 1, 0),BackgroundColor3 = Color3.fromRGB(172, 18, 18):Lerp(Color3.fromRGB(37, 172, 16),Amount)}):Play();
			TweenService:Create(v.Frame.Bar.Behind, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Size = UDim2.new(Amount, 0, 1, 0),BackgroundColor3 = Color3.fromRGB(70, 6, 6):Lerp(Color3.fromRGB(15, 70, 4),Amount);}):Play();
			
			v.Frame.Bar.Visible = Health > 0 and Health < v.Parent.Parent.Humanoid.MaxHealth;
			v.Enabled = Health > 0;
		end);

		local Connection = {
			["Character"] = v.Parent.Parent;
			["Gui"] = v;
			["Connection"] = Update;
		};
		table.insert(self.Collection, Connection);
	end;
end;

--

return humanoidControl;
1 Like

Isn’t it reconnected upon them rejoining?

No
Basically how it works whenever the a players character is added it adds a connection and whenever their character is removed it disconnects the connection and I added a print to test earlier to see if the connection is removed when leaving and it didn’t print so I am concerned if this will cause a data leak.

1 Like

When a Player leaves the game, :Destroy() is called on their player object, but as far as I can find it isn’t properly called on the Players character itself. I can’t test it at the moment as I don’t have access, but you’ll most likely have to manually disconnect the connections related to the humanoid when a player leaves (or dies, possibly).