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

I keep getting an error when I don't see any red "lines"?

Asked by 9 years ago

I get this error in output: 17:41:04.203 - Workspace. .Talk.Script:3: attempt to index local 'plr' (a nil value) but I don't see any of those "red lines" in the script, and I have no idea why its saying plr isn't a variable.

Script:

script.Parent.Touched:connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if hit.Parent.ClassName == "Hat" and plr.PlayerGui.Talk then
        print("...")
    else
        h = hit.Parent.Humanoid
        if h ~= nil then
            game.ReplicatedStorage.Talk:Clone().Parent = plr.PlayerGui
        end
    end
end)

1 answer

Log in to vote
1
Answered by 9 years ago

Red lines only appear when there is a syntax error. Blue lines appear when you use a deprecated or nonexistent variable.

Your problem is that the GetPlayerFromCharacter method is returning nil. If something that is not a character touches the part that method will not be able to find the player and will return nil.

You will have to modify the below code because if plr is found then hit.Parent cannot be a Hat therefore it will never be called.

I also added FIndFirstChild to prevent future errors.

script.Parent.Touched:connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
        if hit.Parent.ClassName == "Hat" and plr.PlayerGui:FindFirstChild("Talk") then
            print("...")
          else
             h = hit.Parent:FindFirstChild("Humanoid")
            if h ~= nil then
            game.ReplicatedStorage.Talk:Clone().Parent = plr.PlayerGui
        end
    end
end)
Ad

Answer this question