Why is CharacterAdded not working?

Hi, so I have a customization screen where players can change the looks of their avatars. Anyways, I have a CharactedAdded() function that waits for the character to load and then executes code below it. But for some reason my CharacterAdded() will not fire for 50% of the time in Play Solo mode which is actually literally infuriating. Does anyone know the reason for this and how to fix it- aka make CharacterAdded() fire instead of doing nothing? Here is a bit of my code to help.

game:GetService("Players").PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(character)

print("ON PLAYER JOIN SCRIPT WAS ACTIVATED!")
	
local charWait = Player.Character or Player.CharacterAdded:Wait()

– A bunch of irrelevant functions below

2 Likes

you don’t need to wait for the character inside the of the CharacterAdded function, the CharacterAdded function gets called when the character is loaded.

I know that is supposed to make sense, but the wait inside makes it run with a 40% better success rate to fire. It’s weird but i’ve tested it, it actually helps the code run which in most cases does NOT for goodness knows why.

well then just get rid of the Player.Character added and just keep that wait line in there.

Can you post a whole script or some more code?

This may also help you. Player.CharacterAdded() event not working?

I think it happens because the game takes longer to load the map than the character, so the character loads first.
Add Player:LoadCharacter() above the function and disable CharacterAutoLoads.

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player:LoadCharacter()
	Player.CharacterAdded:Connect(function(character)

You could also leave the code out of the function and put the script in StarterCharacterScripts.

The rest of the code is either wait lines or functions that fire properly when the script wants to work. Also, setting CharacterAutoSpawn() to false and using Player:LoadCharacter() just makes me spawn in the middle of the blue sky box with nothing to see. Am I doing it wrong? Thanks.

I’m going to link this here bc it sounds like this is what your issue is (90% of the time this happens it is actually this case).

1 Like

Tried this but it still doesn’t fire the code 100% of the time. At first it was okay, but then it stopped making it work. Thanks though. EDIT: NVM, I think it works but I have no idea how to access player.

If that’s not working then however you’re actually hooking up the player added event is wrong or something before it is yielding potentially.

No, I think it stopped working because it couldn’t access the Player variable I put (char, player) for some reason… then it stopped executing.

I have no idea what (player,character) variable you’re talking about. If you’re talking about event arguments (the argument in your connected function), then those will absolutely never be nil, nor inaccessible.

Make sure you check when you’re connecting the player added function. A simple print before the connecting line is a sure-fire way to tell if it’s connected or not.

It’s this: local function CharacterAdded(char, Player) I added Player but it doesn’t even access it- it’s nil. Also I have more test print code in there than a minefield… But it works now, TY.

So the issue was that you just weren’t passing the argument to your CharacterAdded function? That wouldn’t explain the 50% hit-or-miss scenario.

No, it works fine now but the code you gave me didn’t include player in CharacterAdded() so I decided to send player as an argument into the parameters for CharacterAdded() but it came up as nil, so I just used GetPlayerFromCharacter() and it works like a charm now. The reason it didn’t execute code before was because I had a line of print to test if player was successfully transferred but it wasn’t so it stopped the code. So then I just used GetPlayerFromCharacter() and it works now perfectly. Thank you.

Yeah typically you would have to adjust generic code to suit your needs. All you’d have to do in this case is to hookup the CharacterAdded function as an anonymous function in the PlayerAdded function and then pass the player argument into CharacterAdded.
Typically I’d consider using GetPlayerFromCharacter in this case bad practice since you already have the player value at hand here (you need the player to get the character). But regardless of that, it sounds like you got it working. :slight_smile: