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