How to detect when a player has fully connected all the required events in the local scripts

I recently encountered an issue where crucial parts of my game wouldn’t work when I first load in after I set my latency to be very high, and the issue that I think is occurring is that the server is spawning my character before it can connect the required events.

Bascially, I have a respawn timer script on the server that I got from the documentation of Players.CharacterAutoLoads property

local Players = game:GetService("Players")

-- Set CharacterAutoLoads to false
Players.CharacterAutoLoads = false

-- Remove player's character from workspace on death
Players.PlayerAdded:Connect(function(player)
	while true do
		local char = player.CharacterAdded:Wait()
		char.Humanoid.Died:Connect(function()
			char:Destroy()
		end)
	end
end)

-- Respawn all dead players once every 10 seconds
while true do
	local players = Players:GetChildren()

	-- Check if each player is dead by checking if they have no character, if dead load that player's character
	for _, player in pairs(players) do
		if not workspace:FindFirstChild(player.Name) then
			player:LoadCharacter()
			print(player.UserId .. " Spawned")
		end
	end

	-- Wait 1 seconds until next respawn check
	task.wait(1)
end

Here I set the wait timer to be a really short 1 second.

I then added this local script to StarterPlayerScripts

local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer

print("Before Connecting Event")
plr.CharacterAdded:Connect(function(char)
	print(char.Name .." has been added")
end)
print("After Connecting Event")

When I played the game with the incoming replication lag set to 0, the log came out looking like this:

22:49:04.138 Before Connecting Event - Client - LocalScript:5
22:49:04.138 After Connecting Event - Client - LocalScript:9
22:49:04.417 2450058627 Spawned - Server - RespawnTimer:24
22:49:05.706 Academical1721 has been added - Client - LocalScript:7

but when I set the lag to 1.5 seconds, I got this:

22:50:13.119 2450058627 Spawned - Server - RespawnTimer:24
22:50:16.035 Before Connecting Event - Client - LocalScript:5
22:50:16.035 After Connecting Event - Client - LocalScript:9

what was confusing was that when I disabled the RespawnTimer script, even with high latency, the event would still fire properly, is this a property of the Players.CharacterAutoLoads property? And if so, is there a simple way for me to manually detect when a player has connected the CharacterAdded events? I can think of a solution involving making the server wait for the player to fire remote events for every Event that I need to connect before first spawning the player, but that seems like it would be pretty cumbersome in the long run.

1 Like

You can try using .CharacterAppearanceLoaded to try and make the server wait until the character is fully loaded.

But here the local scripts are in the player object, not their character, would .CharacterAppearanceLoaded still work in that sense?

I believe that it is supposed to be use on Player instances in the first place, according to the docs.

1 Like

I tested it out, and it seems like the CharacterAppearanceLoaded only fires after the character is spawned in, not when all local scripts have finished replicating.

Something I’ve done before is instead of doing stuff on PlayerAdded, I make a RemoteEvent called Ready and have the server connect to that. Then, the client will fire the event when it has everything connected/loaded.

So instead of this:

Players.PlayerAdded:Connect(function(player)
	while true do
...

It would be this

Ready.OnServerEvent:Connect(function(player)
	while true do
...

Then in the client:

print("Before Connecting Event")
plr.CharacterAdded:Connect(function(char)
	print(char.Name .." has been added")
end)
print("After Connecting Event")
Ready:FireServer()

I don’t know if you like this solution, but it probably works. You’d have to add extra checks to make sure the client only fires the Ready event once though.

3 Likes

Yeah, and I have just started a similar implementation 20 minutes ago, what are the odds?

Thanks for the solution, at least I’m not the only one that had to deal with this issue.

1 Like