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

"Attempt to index local 'char'(a nil value)" Error?

Asked by 7 years ago

Whenever I test this script in Roblox Studio, it works fine. But when I open the client log in the developer console on the actual game, I get this error -

player.secretboy6.PlayerGui.LocalScript:3: attempt to index local 'char' (a nil value)

-and the script fails to work. How do I fix this?

01local player = game.Players.LocalPlayer
02local char = player.Character
03local torso = char:WaitForChild("Torso")
04 
05 
06Pos = Instance.new("Folder",script.Parent.Parent)
07Pos.Name = "Position"
08 
09XPos = Instance.new("IntValue",Pos)
10XPos.Name = "XPos"
11 
12YPos = Instance.new("IntValue",Pos)
13YPos.Name = "YPos"
14 
15ZPos = Instance.new("IntValue",Pos)
View all 64 lines...
0
Players.secretboy6.PlayerGui.LocalScript:3: attempt to index local 'char' (a nil value) secretboy6 68 — 7y
0
Change char to player.Character or player.CharacterAdded:Wait(). Sometimes the character hasn't loaded yet in an actual client to server environment, so when the script tries to locate the character, it errors. CharacterAdded:Wait() will yield for that event if the player.Character is nil. PreciseLogic 271 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago

You need to yield until the player's character loads into the game. If not the code will run too quickly and run before player.Character exist which then makes char a 'nil' value.

Add this:

1repeat wait() until player.Character

Above:

1local char = player.Character
0
Thanks, that helped! I have another error now... It says that Position is not a valid member of Player secretboy6 68 — 7y
Ad

Answer this question