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

Why does this throw an error? "Attempt to index a nil value"

Asked by
Prioxis 673 Moderation Voter
9 years ago

So I made a script that whenever the player joins and their not an admin then they have a GUI that shows instead but whenever I go to test my script

print'Player Blocker'
local admins = {"ProjectRosploit","AlexTheOtaku","Lisforlucas","Ray5069", "Player", "Player1"}
wait(7)
function tableContains(t, value)
    for _, v in pairs(t) do
        if v == value then 
            return true
        end
    end
    return false
end

game.Players.PlayerAdded:connect(function(playa)
    if playa and tableContains(admins, playa.Name) then
      playa.PlayerGui:FindFirstChild("Main").Block.Visible = false -- line has a error? somehow?
      print'Player Allowed!'
      playa.PlayerGui.Loading.Visible = true
else
      print'Player is not allowed in :('
    end
end)

But nothing happens and it says attempt to index a nil value? what does this mean and how do I fix it?

0
It means that it doesn't exist. It's most likely referring to the `.Visible` property. Are you sure your hierarchy is correct? Shawnyg 4330 — 9y
0
yes Prioxis 673 — 9y

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The error complains that you're indexing a nil value. That means you're trying to use nil on the left of a . or : or [].

Thus we have to be worried about one of these being nil:

  • playa.PlayerGui:FindFirstChild("Main").Block
  • playa.PlayerGui:FindFirstChild("Main")
  • playa.PlayerGui
  • playa

We know from the if that playa is not nil (on a side note: spell correctly -- it's easier to type and process words than random abbreviations, since there's precisely one way to spell correctly)

We know that playa.PlayerGui is not nil because if the object doesn't exist, you would get an error, not nil.

For the same reason, we know that playa.PlayerGui:FindFirstChild("Main").Block is not nil.

This means that playa.PlayerGui:FindFirstChild("Main") is nil, and that is our problem.

:FindFirstChild returns nil when the object doesn't exist. Since you aren't doing that check anyway, you shouldn't be using :FindFirstChild at all -- you should just say .Main.

If you do that, you'll get an error: "Main is not a valid member of PlayerGui", which is precisely your problem. It would have been easier to understand the problem if you hadn't unnecessarily used :FindFirstChild.


At the moment the player joins, they will not have their PlayerGui filled with the GUI objects. It is not until the player spawns that that happens.

The simplest solution is to :WaitForChild("Main") instead of :FindFirstChild("Main")

Warning: This will only affect the first time they spawn. If you want this to happen every time they spawn, you should just manage this from inside the GUI. Then you don't have to worry about waiting.

Ad
Log in to vote
0
Answered by
Maxomega3 106
9 years ago

Perhaps whatever "Block" is hasn't loaded yet. I would recommend using :WaitForChild () on both "Main" and "Block"

playa.PlayerGui:WaitForChild ("Main"):WaitForChild ("Block").Visible = false

Answer this question