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

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

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?

2 comments

0

It means that it doesn't exist. It's most likely referring to the .Visible property. Are you sure your hierarchy is correct?

0

yes

2 answers

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.

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

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