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.

game.Players.PlayerAdded:Connect(function(Player)
	
	script.Load:Clone().Parent = Player.PlayerGui --Insert Loading Screen to load assets
	--Variables
	local PlayerFolder = Instance.new("Folder")
	local PlayerState = Instance.new("StringValue")
	local PlayerLevel = Instance.new("IntValue")
	local PlayerCash = Instance.new("IntValue")
	local PlayerTime = Instance.new("IntValue")
	local PlayerColour = Instance.new("StringValue")
	local PlayerEquippedTrail = Instance.new("StringValue")
	local PlayerEquippedOutfit = Instance.new("StringValue")
	
	
	
	--Locations 
	PlayerFolder.Parent = PlayerInfo
	PlayerState.Parent = PlayerFolder
	PlayerLevel.Parent = PlayerFolder
	PlayerCash.Parent = PlayerFolder
	PlayerColour.Parent = PlayerFolder
	PlayerTime.Parent = PlayerFolder
	PlayerEquippedTrail.Parent = PlayerFolder
	PlayerEquippedOutfit.Parent = PlayerFolder
	
	--Names
	
	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"
	--Values 
	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)
	end
	game.ReplicatedStorage.LeaderboardEvents.PlayerState:FireAllClients(Player, false)
	
	--Local Functions
	---ChangeColour
	--Event Functions
	
	---State Changed (From Client)
	game.ReplicatedStorage.PlayerEventsAndFunctions.StateChange.OnServerEvent:Connect(function(TargetPlayer,State)
		if TargetPlayer == Player then
			PlayerState.Value = State
		end
	end)
	
	---Player Wants To Chnage Colour
	game.ReplicatedStorage.PlayerEventsAndFunctions.SwitchUp.OnServerEvent:Connect(function(TargetPlayer)
		if TargetPlayer == Player then
			local Items = Player.Character:GetChildren()
			ColourChange(PlayerColour.Value)
		end
	end)
	
	--Character Spawns
	Player.CharacterAdded:Connect(function(Character)
		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
print("Character:",player.Character);
Player.CharacterAdded...

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");
end;

if player.Character then charConnect(player.Character); end;
Player.CharacterAdded:Connect(charConnect);
...
5 Likes

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