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

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

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?

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


Pos = Instance.new("Folder",script.Parent.Parent)
Pos.Name = "Position"

XPos = Instance.new("IntValue",Pos)
XPos.Name = "XPos"

YPos = Instance.new("IntValue",Pos)
YPos.Name = "YPos"

ZPos = Instance.new("IntValue",Pos)
ZPos.Name = "ZPos"

while 1==1 do
	X = torso.Position.X
	Y = torso.Position.Y
	Z = torso.Position.Z
	wait()
	XPos.Value = X
	YPos.Value = Y
	ZPos.Value = Z
end

a = nil
function Jay()
	local X = script.Parent.Parent.Position.XPos.Value
	local Y = script.Parent.Parent.Position.YPos.Value
	local Z = script.Parent.Parent.Position.ZPos.Value

	if a == nil or a == true then	
		if X >= -5 and X <= 5 then
			if Y >= 2 and Y<= 10 then
				if Z >= 190 and Z <= 202 then
					for i=1, 0.5, -0.1 do
						script.Parent.Parent.PlayerGui.ScreenGui.PressFTo.TextTransparency = i
						wait()
					end
					a = false
					script.Parent.ScreenGui.chatFrame.FirstConversation.Disabled = false
				end
			end
		end
	end

	if a == false then
		if not(X >= -5 and X <= 5) or not(Z >= 190 and Z <= 202) then
			for i=0.5, 1, 0.1 do
				script.Parent.Parent.PlayerGui.ScreenGui.PressFTo.TextTransparency = i
				wait()
			end
			a = true
			script.Parent.ScreenGui.chatFrame.FirstConversation.Disabled = true
		end
	end
	---------------------------
end

XPos.Changed:connect(Jay)
YPos.Changed:connect(Jay)
ZPos.Changed:connect(Jay)

2 comments

0

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

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.

1 answer

✓ Accepted answer

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:

repeat wait() until player.Character

Above:

local char = player.Character

1 comment

0

Thanks, that helped! I have another error now... It says that Position is not a valid member of Player

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