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?

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

repeat wait() until player.Character

Above:

local 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