Can't get Character from Player

So this is really weird to me and there is probably a simple answer but I am having a problem with getting the character from a player in a local script.

This is my code:

local player = game.Players.LocalPlayer
local character = player.Character or  player.CharacterAdded:Wait()
print(character.Name)

It wont get the character for some reason. I have looked at other posts and the code im using now is the most common answer among them. I tried using waitforchild before but that gave an infinite yield on the character.

I added a print for debugging but for some reason, it outputs the player name instead of the character name

I have never had this problem. I have also tried restarting Roblox Studio.

Any help to this topic would be greatly appreciated

1 Like

When I tried it out in Roblox Studio, it seemed to work fine for me without the “or” part.

Try this and tell me if you have any errors:

local player = game.Players.LocalPlayer
local character = player.Character
print(character)

Also, sometimes if THAT doesn’t work, I add a task.wait() value right before the character retrieval to make sure the character fully loads into the game. Try this as well:

local player = game.Players.LocalPlayer

task.wait(0.01)

local character = player.Character
print(character)

Well, the code looks fine. It’s printing the player’s name because the character is called the same thing as the player. What do you mean “it doesn’t get the character”? Are there any errors?

@reckride2700 all the or means is if the character is nil (not loaded yet in this case) it will use the Wait method of the RBXScriptSignal CharacterAdded, which will return the player’s character once it loads. Using such a statement is better. I noticed you included a task.wait statement, but that may not stop the issue of the character not loading in time depending on a player’s network and device stability and speed.

Thanks for the tip!

Also, you’re correct about the character name being the same as the player name.

@SaltRocketYT, If you want to REALLY be sure that the character is being called and not the player, try adding a simple character:Destroy() to the end of the script and running it once in Roblox Studio.

If it’s truly calling the player, then your character should disappear, and you know you coded things correctly.

the or expression is correct you shouldn’t tell him to remove it

When using wait or not, it printed “nill”

When using my code, it doesn’t give any errors. Thanks for the info about the character being called the same as the player. I didn’t know that and that is very helpful to this problem. The print returns as nill even when using “player.CharacterAdded:Wait()” and gives an infinite yield when attempting to get the character via “WaitForChild”. I also tried print(character.Parent.Name) but it gives the error: “attempt to index nill with Name”

Just curious, where is the script located? I tested your original script in StarterCharacterScripts and it seemed to work completely fine, so I’m wondering if it’s a problem with the location of it.

The script was located in StarterPlayerScripts. When putting it in StarterCharacterScripts, it worked! Thank you so much. I guess I forgot to learn when to use each

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.