Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Character a nil value?

Asked by 8 years ago

So I tried this in a Local Script:

player=game.Players.LocalPlayer
char=player.Character

but then I reference it later and gives me the 'nil value'.

I even tried this:

player=game.Players.LocalPlayer
char=player:WaitForChild("Character")

and that didn't do anything at all. Any suggestions?

0
Wait for LocalPlayer too. HungryJaffer 1246 — 8y
0
You don't need to wait for LocalPlayer. LocalScripts can only get LocalPlayer from descendants of the player, so therefore the player joins the game before the scripts even fire. dyler3 1510 — 8y

2 answers

Log in to vote
2
Answered by
dyler3 1510 Moderation Voter
8 years ago

Because Character isn't actually a direct Child of Character, you can't use :WaitForChild. You simply need a loop to wait for it to get it the game. In this case, we'll use a repeat loop to wait until the player is found:


Here's something you could use:

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

There you go. Anyways, hope this helped. If you have any further problems/questions, please leave a comment below. Hope I helped :P

-Dyler3

0
Thank you once again NAWESOME14 40 — 8y
0
No problem. glad I could help :P dyler3 1510 — 8y
Ad
Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

The WaitForChild function is used to wait until a specified child appears in the class you called the function on. Due to Character being a Property, rather than a child.. it renders this function inoperable.

There are two ways that you could wait until the character is no longer nil.

  • 1) CharacterAdded event with the wait function.

    Events have three viable methods. One of them being wait. If used, the current thread will yeild until the event fires. In your case.. will yeild the current thread until the character is no longer nil

local plr = game.Players.LocalPlayer

plr.CharacterAdded:wait()

local char = plr.Character
  • 2) A loop

    You can use one of the two endless loops; while or repeat.. to repeatedly wait until the character is no longer nil.

local plr = game.Players.LocalPlayer

repeat wait() until plr.Character

local char = plr.Character
0
Upvoted :P dyler3 1510 — 8y
0
Same Goulstem 8144 — 8y

Answer this question