Why does this not print sometimes?

It should be working, but something else is hindering it. When does it not fire? Does it fire consistently when you reset the character?

1 Like

Yes, I think the only times when it doesn’t load is whenever I first join.

1 Like

Sometimes the PlayerAdded event fires before the script connects the signal. That’s why you should loop through existing players as well.

1 Like

What? I’m confused of what you mean by that. Could you maybe explain what you mean?

1 Like

It is simply because CharacterAdded event sometimes doesn’t fire right after PlayerAdded, which is quite odd in my opinion. Try to look for the player’s character first by Player.Character, otherwise fall back to Player.CharacterAdded event.

1 Like

That’s what I was thinking maybe the character already exists, so I added a print where it checks if Player.Character exists then prints it when the players added to see if it exists + doesn’t fire. I’ll get back to you if it doesn’t print though.

1 Like

Make sure the player’s character doesn’t already exist:

game.Players.PlayerAdded:Connect(function(Player)
    if not Player.Character then Player.CharacterAdded:Wait() end
    print("test")
end)
2 Likes

That doesn’t really answer why CharacterAdded doesn’t run occasionally.

Yeah cause its already been answered

game.Players.PlayerAdded:Connect(function(Player)
	local Character = Player.Character or Player.CharacterAdded
	print("Character Exists")
	Player.CharacterAdded:Connect(function()
		print("Character Spawned")
	end)
end)

Okay, but what you just typed down doesn’t really make any sense other than it being a slight more efficient method than the guy that just posted above you.

(and it has nothing to do with the topic)

I still don’t know why your code wouldn’t work, but NachtHemd’s is the first solution I would try. Here is an example.

local function CharacterAdded(Player, Character)
    print'test'
end

local function PlayerAdded(Player)
    Player.CharacterAdded:Connect(function(Character) CharacterAdded(Player, Character) end)
    if Player.Character then CharacterAdded(Player, Character) end -- Get any characters that loaded before the script ran
end

game.Players.PlayerAdded:Connect(PlayerAdded)
for _, Player in game.Players:GetPlayers() do PlayerAdded(Player) end -- Get any players that somehow loaded before the script ran.

That script seems a bit inefficient, what you’re doing is looping through players each time the player joins and connecting a characteradded event which is just the same thing. It also looks the same to me, but more overcomplicated.

You check if there’s a character, then connect the function which is probably all you really need to do in the first place.

Wrong. I loop through players only once, when the script loads. That way, if the script is somehow loading after a player has already joined, it will round up all of the players currently in the game and then connect to PlayerAdded to get all future players. Connections only work for future events, I account for the past as well. Efficiency will not take any noticeable hit.

Edit: I admit though, it’s a stretch. Especially with the problem as sporadic as you claim, I doubt it has anything to do with a missed connection. Again though, the first thing I would try.

Oh fr? But most people just do it the way that I did, except theyd probably just added a check for the character then run the print.

I don’t disagree. But then I’ve never had a problem doing that either. Solving problems while either programming or driving a forklift is a lot of messing around until something clicks. So with that, I don’t think it will help, but it won’t hurt anything either.

Yeah I guess we’ll see once I figure out if it doesn’t print for characteradded, but prints the player’s character already exists in playeradded.

1 Like

Sleitnick has an entire 7 minute video dedicated to this issue especially with the character added scenario. Both @JarodOfOrbiter and @NachtHemd touch on this issue as well with the scenarios so yeah I suggest watching the video for a clearer explanation.

In my experience, this event doesn’t always fire. This might have changed after they updated the character added ordering. What I do is repeat wait() until Player.Character, as well as the event.

Don’t do that, just use local Character = Player.Character or Player.CharacterAdded:Wait() instead, it’s much more efficient.