Do I need to disconnect this stuff?

Hello. I have a PlayerManager module, it controls events for every player such as:
Joining, leaving, loading, data, remotes, etc

Is this code I have fine? Or should I not be disconnecting these?

Ignore the playerJoinConnection and playerLeaveConnection

       playerJoinConnection = Players.PlayerAdded:Connect(function(player)
			print("\nPlayer joined. Username:", player.Name)
			
			Manager.playerStorage[player.Name] = {}
			local PlayerTable = Manager.playerStorage[player.Name]
			PlayerTable.Connections = {}
			PlayerTable.Stats = {}
			PlayerTable.Stats.isSpawned = false
			local Character = player.Character or player.CharacterAdded:Wait()
			
			PlayerTable.Connections.characterRemovedConnection = player.CharacterRemoving:Connect(function(character)
				humanoidConnectionRemote:FireClient(player, true)
				PlayerTable.Stats.isSpawned = false
			end)
			PlayerTable.Connections.characterAddedConnection = player.CharacterAdded:Connect(function(character) -- TEMP
				humanoidConnectionRemote:FireClient(player, false)
				PlayerTable.Stats.isSpawned = true
			end)
			
			
		end)
		
		playerLeaveConnection = Players.PlayerRemoving:Connect(function(player)
			print("\nPlayer leaving, disconnecting connections.")
			for _, connection in Manager.playerStorage[player.Name].Connections do
				connection:Disconnect()
			end
			
			table.clear(Manager.playerStorage[player.Name])
			Manager.playerStorage[player.Name] = nil
		end)

This isn’t the entire script, only the part I have questions for, I don’t want to leave my source for everything else out
The other stuff isn’t connections so it wouldn’t matter anyway

1 Like

You don’t have to disconnect those, infact you shouldn’t even be storing them as they automatically disconnect when the player leaves.

2 Likes

Alright thanks, I am experimenting with trying to organize stuff lol
Should I clear their table when they leave though? Since there’s more than just connections, if they aren’t cleared can it cause lag for old servers

Yes, you can just change your code to this:

playerJoinConnection = Players.PlayerAdded:Connect(function(player)
			print("\nPlayer joined. Username:", player.Name)
			
			Manager.playerStorage[player.Name] = {}
			local PlayerTable = Manager.playerStorage[player.Name]
			PlayerTable.Connections = {}
			PlayerTable.Stats = {}
			PlayerTable.Stats.isSpawned = false
			local Character = player.Character or player.CharacterAdded:Wait()

            player.CharacterRemoving:Connect(function(character)
				humanoidConnectionRemote:FireClient(player, true)
				PlayerTable.Stats.isSpawned = false
			end)

            player.CharacterAdded:Connect(function(character) -- TEMP
				humanoidConnectionRemote:FireClient(player, false)
				PlayerTable.Stats.isSpawned = true
			end)		
		end)
		
		playerLeaveConnection = Players.PlayerRemoving:Connect(function(player)
			print("\nPlayer leaving, disconnecting connections.")
			for _, connection in Manager.playerStorage[player.Name].Connections do
				connection:Disconnect()
			end
			
			table.clear(Manager.playerStorage[player.Name])
			Manager.playerStorage[player.Name] = nil
		end)
1 Like