Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this wrong?

local player=game.Players.Localplayer
local char=player.Character

Every time I try to test my game, this error pops up... I don't know how to fix this...

18:59:37.338 - Players.Player1.Backpack.LootCorpse:5: attempt to index field 'Character' (a nil value)

2 answers

✓ Accepted answer

Character is a nil value. Your code doesn't contain enough information to discern why, but let's have a gander:

  • You're not waiting for player.Character to exist. char = player.Character or player.CharacterAdded:wait() will fix that issue, because it makes sure that it waits for your character at all times.

  • You're not using WaitForChild. This is a LocalScript; in the event that Character isn't Player.Character like expected, you'll need to use WaitForChild to make sure that it exists before trying to get any references to it.

2 comments

0

Your 2nd bullet is incorrect. I've tested that out before, and that's the reason my scripts never got past that point. The model 'Character' isn't a child of the Player Instance, which will prohibit the script from advancing beyond that point. In case I flipped my words: 'Character' won't show up as a descendant of Player in the Explorer, so therefor it will not work.

0

My second bullet isn't incorrect, but instead you just can't read. My second bullet is in the event that it's not referring to the Character property of Player and instead a Character child of some other object. There's a whole 3 lines missing, so I have to cover all possibilities.

It seems like the Character is returning before the character actually loads in. Here's a simple fix

local player = game.Players.Localplayer
repeat wait() until player.Character
local char = player.Character

Note: Also make sure this is in a LocalScript, or else you can't call the LocalPlayer

This discussion is preserved as it was. New answers, comments, and votes are closed.