So I’ve always handled characters being added to the game like this:
local function characterAdded(character)
print(‘Character added’)
end
game.Players.PlayerAdded:Connect(function(player)
while not player.Character do wait() end
characterAdded(player.Character)
player.CharacterAdded:Connect(characterAdded)
end)
I’ve been doing it like this because I’m 99% sure I had problems in the past with CharacterAdded not firing the first time a player joins. Now I’ve seen multiple times where people don’t bother with it. And after recently checking, it seems like it does fire every time, but I want to make sure if it’s guaranteed,
or if I’m going crazy and it’s already worked, or if it has something to do with the new avatar loading order that fixed this. Thanks!
The while not player.Character do wait() end characterAdded(player.Character) part of your code is redundant because player.CharacterAdded will fire when player.Character exists.
Just use
player.CharacterAdded:Connect(characterAdded)
It should fire 100% of the time. The only time I see it not firing is if the player’s character is never added or if the code runs after the player’s character has been added (Which should not happen unless your code is on the client). You do not have to worry about the latter if your code is on the server and runs when the server starts.
TL;DR: player.CharacterAdded will fire every time except in special cases such as the character existing before the code is ran and the player’s character never being added in the first place.
You could set your definitions of the character on the client to be the character if it exists, and also call a function when the character is added.
local character = client.Character
if character then
characterAdded(character)
end
client.CharacterAdded(characterAdded)
If you only need a static definition of the character you could shorten the code to something like this
local character = client.Character or client.CharacterAdded:Wait()
This works by setting character to be client.Character if it exists, but if it does not then it will set character to be the value returned from client.CharacterAdded:Wait()
If you did not know, signal:Wait() will return the parameters that would’ve been passed to a function connected to the signal.