Player:LoadCharacter() causes server to completely Freeze for a few seconds

It’s alright, and I appreciate the help. I double checked the beta features, and I’ll also try to disable them all, but I’m unsure if this will solve anything. I could of sworn I tried it already, but I’ll do a once over.

I agree, it’s weird it’s happening to them too. One of my friends they couldn’t observe a spike while others said they were. When there is 4-5 people in one game trying to play competitively, it really ruins the fluidity of combat, and I’d love to get this resolved soon.

edit: it didn’t change anything at all, sadly.

2 Likes

I could be wrong, but it doesn’t sound at all like :LoadCharacter is the cause of this in itself, but rather whatever your scripts are doing upon player spawning, player.CharacterAdded

1 Like

LoadCharacter has its problems and issues. I used to use LoadCharacter in Bloxy Kart v3.5 and older, and this exact issue occurs only sometimes. Probably an engine issue more than an issue in your code, because after a while, it fixes itself.

What we did was avoid it all together by using alternatives. We didn’t actually need LoadCharacter(), so we got rid of it and instantly set the players character with a default model if needed.

If possible, I would try temporarily setting the player’s respawn time to 0 temporarily, then Player.Character.Humanoid.Health = 0, then setting the respawn time back.

How to fix for your experience.

Step 1: Add a RemoteEvent in ReplicatedStorage called “OnLoadForClient”
Step 2: Add a RemoteFunction in ReplicatedStorage called “QuickRespawn”
Step 3: Add this function and code to the place where you would like to call Player:LoadCharacter()

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local quickRespawn = ReplicatedStorage.QuickRespawn
local onLoadForClient = ReplicatedStorage.OnLoadForClient

function loadCharacter(Player: Player)
	task.spawn(function() -- We need this so we do not cause any infinite loads if the player leaves while we are loading the character. Removing it will cause issues if the player leaves when this function is called.
		local character = Player.Character or Player.CharacterAdded:Wait()
		local humanoid: Humanoid = character.Humanoid

		quickRespawn:InvokeClient(Player) -- This script is designed to Invoke the client safely. Do not invoke the client unless you absolutely know how to script it safely.

		humanoid.Health = 0
		
		onLoadForClient:FireClient(Player) -- Tells the client to fix the respawn time so you do not have instant respawns.
	end)
end

Step 4: Add this code in a LocalScript to StarterPlayer.StarterPlayerScripts:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local quickRespawn = ReplicatedStorage.QuickRespawn
local onLoadForClient = ReplicatedStorage.OnLoadForClient

local defaultRespawnTime = Players.RespawnTime

quickRespawn.OnClientInvoke = function()
	Players.RespawnTime = 0
	
	return -- Tells the server that it is ready to kill
end

onLoadForClient.OnClientEvent:Connect(function()
	Players.RespawnTime = defaultRespawnTime
end)

Step 5:
Substitute Player:LoadCharacter() for loadCharacter(Player)

1 Like

I am not yielding the CharacterAdded event or doing anything complex. All I’m doing is copying accessories based off the equipped character skin. Also, even if this were the case, why does it make the entire server plummet in terms of performance? (e.g: The entire task scheduler in Studio completely grinds to a halt).

Is there some way that I could be achieving that terrible of performance?

1 Like

I’m going to test this issue on my own. If I can reproduce, I’ll make a bug report.

I’ve tested the issue and I can’t seem to reproduce on a blank baseplate. It’s likely due to the fact that you have some unoptimized code which are connected to CharacterAdded functions. Let me give this thread a look one more time and I’ll see if there’s enough information for me to provide any advice.

Edit #2: Okay, here are your options. You can make a bug report, even if it’s in your specific place, and have Roblox staff look into it. However, before you do that, can you send any script that handles a character-added event? If you want to talk on Discord, it’s the same as my roblox username, if that makes you more comfortable. If you still can’t seem to resolve the issue, then you can make a bug report, and give roblox staff a reprod file, though it may take a while for a staff member to get to it.

1 Like

Not sure if this helps, but as I said before, new games don’t seem to be heavily impacted as the performance seems to scale with the size of the game. This bug was actually a thing in 2016 initially, and Roblox claims to have fixed it. The guy in that post appears to be experiencing the exact same issue I am in the present day.

Also keep in mind that someone else has posted a similar problem whenever someone respawns at the very bottom of the thread, just recently too.

LoadCharacter in the past used to instantly respawn the character with very little delay, but you can notice a small skip in the time it takes for the Character to at least load.

That gives me an idea then:

Can you run a MicroProfiler benchmark just for the Player:LoadCharacter() call itself and nothing else?

Literally just wrap your function in debug.profilebegin("LoadCharacter") and debug.profileend() and record the microprofiler. Let me know your results and hopefully we can go from there considering you can only repro from your place file.

This’ll mainly just tell us whether it’s the LoadCharacter call itself or your other scripts that are causing issues.

1 Like

If I were you, I’d first verify that :LoadCharacter() is actually causing the lag. I assume you did this already, but wherever you are calling :LoadCharacter(), remove all of the code surrounding it. If it doesn’t lag anymore, narrow the script down until you find the problem.

If removing all the surrounding code doesn’t work, I’d then try to disable all other scripts in the game. There could be a .CharacterAdded function someone that could be causing the lag. It could also be a .ChildAdded or .DescendantAdded function (I’d use Ctrl + Shift + F to search for these functions).

It could also be an issue with mass part replication, or parts falling into the void. I’d check that out too. How many scripts do you have inside StarterCharacter? Also, do you have CharacterAutoLoads disabled?

I can almost guarantee you this problem is a result of the code and/or parts in your game (since you are revisiting this problem), so I would just keep that in mind.

1 Like

Hey um, I suppose this says a lot? I just wrapped the :LoadCharacter() call using the two debug methods you provided, and this is the result:

Also, is this what you mean by record?

Also it appears right after the LoadCharacter call, this also piles up for a long time it seems. I have no clue what it could mean though.

I tried disabling literally every script that pretty much makes the game, a game, and it still resulted in what I replied with above.

It seems the MicroProfiler is indeed labeling :LoadCharacter() as an issue, but I have no clue how I would fix it if disabling literally all my scripts resulted in close to nothing.

Here is the documentation on the command. It’s worth a read, shows a few cases you may have problems with it. Also shows it can be resource-intensive …

Possibly chooses your moments to use it to avoid stalling. Also you could try to defer the task …

task.defer(function()
     player:LoadCharacter()
end)

This can help to cut down bottle necking. Basically lowering the task priority.

1 Like

It seems deferring the task bumps up the total delay by a few extra milliseconds. I appreciate the assistance, but I already took a look at this article. I usually do to verify if anything I’m doing may be incorrect.

Hmm, I was thinking it may be this from that link …

After calling LoadCharacter for an individual player, it is not recommended to call it again for the same player until after that player’s Player.CharacterAppearanceLoaded event has fired.

CharacterAppearanceLoaded does not fire if CharacterAutoLoads is false I thought? And even if it was because of HumanoidDescriptions, those are only being used for body colors honestly. That alone shouldn’t be causing that huge of a spike.

I appreciate the help again, though.

I think you have enough to make a bug report. If you don’t have access to the group, feel free to DM me and I’ll post on your behalf, either that or you can message the @Bug-Support group and it will be approved by a review team.

Do you happen to know how long bug support requests last?

Also, would you like me to format my bug report like an actual bug report so you can just copy the contents and make it actual post?

Thanks in advance.

Also, I’d like to note that between the two experiences, my spawn script was never changed much at all. All I really did was add a few extra spaces in-between functions and condition checks.

I closely compared the two code bases, but to no avail. Also, I feel like me disabling literally all my games code should be saying something about the issue.

Having the same problem, i’ve worked in a fighting arena game with custom characters
and when people spawns in, the server cpu usage increases up to 100%

1 Like

worth a shot to disclose but here:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)

--This for loop here is needed as character added fires multiple times when activating morph changes, needs to stay here to prevent mass cloning
		if table.find(RespawnTable, player.Name) then
			for i, v in pairs(RespawnTable) do
				if v==player.Name then
					table.remove(RespawnTable, i)
					break
				end
			end
			return
		end

table.insert(RespawnTable, player.Name)

this is a loop I did in one of my morph systems as it would clone upwards to 6 times on .CharacterAdded without the loop

as others suggest its likely characteradded but… unless a minimal reproduction file is given, we can’t truly figure out for sure.

if doing loadcharacter on clean file does not net the same effect then its something about the game file

Depends.

If you’re referring to applications to the bug reports group for permission to post, it could be from a week to a month.

If you mean the request to post a bug report via the bug support group: Roughly a few days for a response, give or take a week.