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

Character a nil value?

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?

2 comments

0

Wait for LocalPlayer too.

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.

2 answers

✓ Accepted answer

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

2 comments

0

Thank you once again

0

No problem. glad I could help :P

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

2 comments

0

Upvoted :P

0

Same

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