Why does this not print sometimes?

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.