CharacterAdded event doesnt fire on first join

for my main game script when a player joins it does some stuff and then inside that playerAdded function i have Player.CharacterAdded but sometimes when i join the game the characteradded event doesnt fire. is this because of some lag or something else.

	script.Load:Clone().Parent = Player.PlayerGui --Insert Loading Screen to load assets
	local PlayerFolder ="Folder")
	local PlayerState ="StringValue")
	local PlayerLevel ="IntValue")
	local PlayerCash ="IntValue")
	local PlayerTime ="IntValue")
	local PlayerColour ="StringValue")
	local PlayerEquippedTrail ="StringValue")
	local PlayerEquippedOutfit ="StringValue")
	PlayerFolder.Parent = PlayerInfo
	PlayerState.Parent = PlayerFolder
	PlayerLevel.Parent = PlayerFolder
	PlayerCash.Parent = PlayerFolder
	PlayerColour.Parent = PlayerFolder
	PlayerTime.Parent = PlayerFolder
	PlayerEquippedTrail.Parent = PlayerFolder
	PlayerEquippedOutfit.Parent = PlayerFolder
	PlayerFolder.Name = Player.Name
	PlayerState.Name = "PlayerState"
	PlayerLevel.Name = "PlayerLevel"
	PlayerCash.Name = "PlayerCash"
	PlayerColour.Name = "PlayerColour"
	PlayerTime.Name = "PlayerTime"
	PlayerEquippedTrail.Name= "PlayerEquippedTrail"
	PlayerEquippedOutfit.Name = "PlayerEquippedOutfit"
	PlayerState.Value = "LoadingScreen"
	PlayerColour.Value = "Yellow"
	PlayerTime.Value = 0
	PlayerLevel.Value = 1
	PlayerCash.Value = Player:WaitForChild("PlayerInfomation"):WaitForChild("Cash").Value
	local DataStore = game:GetService("DataStoreService")
	local Equipped = DataStore:GetDataStore("EquippedDataStorage")
	local Equipment = Equipped:GetAsync(Player.UserId) or {Trail = "DefultTrail", Outfit = "DefultOutfit"}
	local MaxLevel = 8
	PlayerEquippedTrail.Value= Equipment["Trail"]
	PlayerEquippedOutfit.Value = Equipment["Outfit"]
	local Players = game.Workspace.PlayerInfomation:GetChildren()
	for i = 1,#Players do
	game.ReplicatedStorage.LeaderboardEvents.NewPlayersInfo:FireClient(Player, Players[i].Name, Players[i].PlayerLevel.Value, Players[i].PlayerTime.Value)
	game.ReplicatedStorage.LeaderboardEvents.PlayerState:FireAllClients(Player, false)
	--Local Functions
	--Event Functions
	---State Changed (From Client)
		if TargetPlayer == Player then
			PlayerState.Value = State
	---Player Wants To Chnage Colour
		if TargetPlayer == Player then
			local Items = Player.Character:GetChildren()
	--Character Spawns
		print("Player character spawned")

Heres the script incase it helps. sorry if theres any weird prints in it i forget what i add

I would do this:

--Character Spawns

This will print Character: nil if they have no character, or Character: [redacted].
So you can see if their player was created before the event was able to fire.

Ye when it brakes it outputs:

Character: [redacted]

as expected. But how do i fix it?

the event most likely gets fired before the connection gets set up since your doing it after all those other things so try setting up the event connection before you do anything else, also your calling a yielding function so move the character added event to the top of the body within the player added event.

Psst "This was confirmed with the code I had him run :stuck_out_tongue_winking_eye: "
@anon25856735 The easiest fix is to just set the function inside your connection to a named function rather than an anonymous one. And call it if the player’s character already exists.

--Character Spawns
function charConnect(character)
  print("player spawned");

if player.Character then charConnect(player.Character); end;

Is this a LocalScript by any chance? I see the following code
script.Load:Clone().Parent = Player.PlayerGui

If it is a LocalScript and the LocalScript resides in ReplicatedStorage, the first time the character loads it will not fire the event as most often than not the character loads before the event can fire, so instead you should place the LocalScript inside StarterCharacterScripts and change your code from there, as then the LocalScript will always load alongside the Character.

Thank you, im kinda mad i didn’t think of this myself.

1 Like