Client Freeze onJoin + PlayerAdded Memory Leak

Hello,

I hope that everyone reading this is doing well. Recently, while I’ve been working on my game, there has been a game-killing issue where the client will freeze onJoin. If I am already playing in a server and someone joins, they will shortly leave and rejoin, and when I ask them if they rejoined because their client froze, they always say yes. It has happened to me several times too; I will join a pre-existing server but my client will freeze. It happens both before, during and after the default loading screen fades away. However, the in-game music still plays! In addition, my mouse icon is stuck at 0,0.

If I try to leave the game, my character will still be loaded in as if the server doesn’t realize that I left until like twenty seconds later. If I rejoin while my character is still in, there will be a warning message saying “you’re already playing on another device” or something like that. This bug sounds like a client-sided issue since the server itself isn’t freezing though I have yet to find any localscripts that could cause this issue.

What I’ve tried to do

The map can generate anywhere between 8000 to 20000 parts; however, even when the MapScript is disabled, I’ve still had this issue.

I spent an entire day a week or two ago going through every script and localscript in the game to find any memory leaks. To my knowledge, there aren’t any memory leaks or server-client bridge issues except for one thing… the PlayerAdded event has a memory leak, I believe. since I fixed that problem thanks to:

The only reason why I haven’t marked this thread as solved is because the freezing issue is still a problem that plagues several people at random times.

Any help is appreciated! This is a serious issue, and I would love to move past this as soon as possible. Thanks for reading all of this. Cheers ~ Eko

2 Likes

Problem is that you are creating a new PlayerRemoving connection every time a player joins when what you should do is move that event outside the scope of PlayerAdded event:

game.Players.PlayerAdded:Connect(function(player)
	-- A bunch of datastore Getting and Setting
	-- A Chat Command Function
end)

game.Players.PlayerRemoving:Connect(function()
	print'bye' --checking for memory leak
end)
1 Like

Alright, I just tested it, and there’s now only one “bye”. Thank you for that! As far as the client freezing, do you think that was the cause or is there something else going on?

I don’t think multiple PlayerRemoving events can cause a problem like that but I’m not sure what causes that, sorry.

No worries; thank you anyway :~)

bumping this so it won’t get buried before a solution is found (not sure if people bump on this platform like the old Roblox forums; I’m still new)

It’s probably another script causing the memory leak. Do you use any plugins?

Also look into Script Performance in Studio.

No, I haven’t used any plugins besides Roblox’s animation editor. Also, I just tried to go into a server to check out Script Performance, and it crashed before I could even do anything.

As for trying it in Studio, if I publish my game to the website and try to Play Solo, when I exit Play Solo, there’s a good chance that it’ll crash there too. I’m not sure if that’s related or not.

But now that I’m loaded in, these stats don’t look too out of the ordinary. But then again, I could be wrong since this is the first time that I’ve had to really crack down on this. Thanks for the help.

I also checked the logs. Here’s what I found at the very end of the logs from when I said earlier today that my client crashed:

2021-01-26T18:01:23.765Z,72.765001,36d0,7 Disconnect reason received: 266
2021-01-26T18:01:23.776Z,72.776035,36d0,7 Connection lost
2021-01-26T18:01:23.776Z,72.776035,36d0,7 Connection lost: connectMode: Disconnect from no ACK, timeMS:585074523, connectionTime 585010421
2021-01-26T18:01:23.776Z,72.776035,36d0,7 Connection lost: AckTimeout 0, IsOutgoingDataWaiting 0
2021-01-26T18:01:23.776Z,72.776035,36d0,7 Disconnection Notification. Reason: 266
2021-01-26T18:06:11.778Z,360.778904,2a6c,6 Info: WatcherThread Detected hang.
2021-01-26T18:06:11.778Z,360.778904,2a6c,6 TaskScheduler::Job: WaitingScriptJob, state: 1, seconds spend in job: 348.102
2021-01-26T18:06:11.778Z,360.778904,2a6c,6 Info: StartProcessException...
2021-01-26T18:06:29.057Z,378.057775,2a6c,6 Info: Uploading crash analytics...
2021-01-26T18:06:29.058Z,378.058777,2a6c,6 Info: Done uploading crash analytics

Reason #266 is “The smart card service is not running. Please start the smart card service and try again”. Not sure what any of this means or if it’s related.

To further confirm that it’s not the loading of thousands of parts onJoin, I enabled Streaming and set the radius to 4096. Way outside the radius at a Y level of 8192 studs, players now join the server in a spawn above the map. Once they press the “play” button on the screen, they are teleported to the lobby where the client then has to load all of the parts from the map and lobby. This was an attempt to prevent the client from having to load so many parts onJoin, but it still freezes from onJoin to onJoin. Here’s the game link: Ultimate Brick Battle 💣 - Roblox.

Sooooo I think I discovered the issue. Found this at the end of a LocalScript deep within StarterGui.

local isPlayingSong = false
while true do
	for i, song in pairs(script.Songs:GetChildren()) do
		if song.Playing == true then
			isPlayingSong = true
		end
	end
	
	if not isPlayingSong then
		if #songs == 0 then
			songs = {}
			for i, song in pairs(script.Songs:GetChildren()) do
				if song.Active.Value == true then
					table.insert(songs, song)
					if song.Name == "Darkest Omen (Chrono Symphonic)" then
						table.insert(songs, song["Darkest Omen [Part 2] (Chrono Symphonic)"])
					elseif song.Name == "The Masamune (Chrono Symphonic)" then
						table.insert(songs, song["The Masamune [Part 2] (Chrono Symphonic)"])
						table.insert(songs, song["The Masamune [Part 3] (Chrono Symphonic)"])
					end
				end
			end
		end
		if lastSong == "Darkest Omen (Chrono Symphonic)" then
			PlaySong(script.Songs["Darkest Omen (Chrono Symphonic)"]["Darkest Omen [Part 2] (Chrono Symphonic)"], true)
		elseif lastSong == "The Masamune (Chrono Symphonic)" then
			PlaySong(script.Songs["The Masamune (Chrono Symphonic)"]["The Masamune [Part 2] (Chrono Symphonic)"], true)
		elseif lastSong == "The Masamune [Part 2] (Chrono Symphonic)" then
			PlaySong(script.Songs["The Masamune (Chrono Symphonic)"]["The Masamune [Part 3] (Chrono Symphonic)"], true)
		else
			local newSong
			repeat newSong = math.random(1, #songs)
			until string.find(songs[newSong].Name, "Part") == nil
			PlaySong(newSong, false)
		end
	end
	isPlayingSong = false
end

There’s a wait(2) in the PlaySong() function. I assumed that if I call PlaySong(), I don’t have to add any waits between while loop intervals. I added a wait(0.5) at the end of the loop and never had an issue again. Here’s what’s interesting:

In the bottom right, you can actually see that no song is playing (there’s supposed to be a song playing) which means that if I didn’t include that wait(0.5), I would’ve frozen again. Of course, now the issue is “why isn’t my playlist playing 100% of the time” but that’s something that I can probably figure out a little later. Thanks again to everyone who checked out this thread!