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?
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
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