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

PlayerGui is a nil value?

Asked by
Spoookd 32
8 years ago

I made a variable for the players so I can get the PlayerGui which is plrs = game.Players:GetPlayers() and I tried to check to see if a gui is visible it will remove it if there are more than 2 players and if not it will stay there. Though an error comes up saying: "ServerScriptService.Script:13: attempt to index field 'PlayerGui' (a nil value)".

plrs = game.Players:GetPlayers()

game.Players.PlayerAdded:connect(function()
    if game.Players.NumPlayers >= 2 and mapInstantiated ~= true then
        for i, v in pairs(plrs.PlayerGui:GetChildren()) do
            if v.Name == "NMP" then
                v.Visible = false
            end
        end
    else
        for i, v in pairs(plrs.PlayerGui:GetChildren()) do
            if v.Name == "NMP" then
                v.Visible = true
            end
        end
    end
    -- Rest of code
0
Sorry about that, try it now. I forgot to edit. dyler3 1510 — 8y

2 answers

Log in to vote
2
Answered by 8 years ago

Iterate

There's something of a trick to doing something to everything, and it's that you have to iterate through your selection. In order to iterate through this, we're going to use a for loop with an iterator function - Let's have a look at how that works.

Iterator loops

Iterator loops have a simple structure:

for ... in iterator, constant, init do
    -- Code
end

But for you, we'll simplify this with an iterator factory

for k,v in pairs(table) do
    -- Code
end

What happens here is that for every time the loop body runs, k becomes the key for the table (In your case it will become the iteration count), and v becomes the value it relates to.

You already make use of an iterator loop in your code. Simply make another one to get each individual player from plrs

0
So I am almost doing the same thing? Spoookd 32 — 8y
0
Loop for the Players and -then- loop for the PlayerGui for each Player. User#6546 35 — 8y
0
Basically, "PlayerGui" is a child of a "Player" object. However, you are trying to retrieve a "PlayerGui" from a *table* or *list* of players (your "plrs" variable). What eLunate suggests here is to loop through each player in the list to access their individual PlayerGui object and *then* loop through their PlayerGui's children to find your Gui. Link150 1355 — 8y
Ad
Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
8 years ago

Your only problem is where you're attempting to get the PlayerGui of each player. This is actually a quite simple fix, you just need to understand how :GetChildren() and pairs work.


I think you pretty much have the right idea about this, but you're going about it the wrong way. The :GetChildren()method returns a table of all descendants in the object (argument in parentheses) that you include.

Pairs is used to iterate through a table of values. This means that you need to make sure that you use a table in the parentheses, which is exactly what GetChildren() returns.


Now, to apply this to your code, we simply need to move a couple things around, understanding what data types we're dealing with.

for i,v in pairs(plrs.PlayerGui:GetChildren()) do

This line of code is incorrect because plrs is a table value, and not an object (you're attempting to get the player's children, not the tables).

To fix this, we need to iterate through each object in the plrs table. Because we need to get each object before running the loop. we would now need to insert another loop:

for i,v in pairs(plrs) do
    for i2,v2 in pairs(v.PlayerGui:GetChildren()) do
        --Code
    end
end

And that's what it would look like!


Here's what your actual code would be:

plrs = game.Players:GetPlayers()

game.Players.PlayerAdded:connect(function()
    if game.Players.NumPlayers >= 2 and mapInstantiated ~= true then
        for i, v in pairs(plrs) do
            for i2,v2 in pairs(v:WaitForChild("PlayerGui"):GetChildren()) do
                if v2.Name == "NMP" then
                    v2.Visible = false
                end
            end
        end
    else
        for i, v in pairs(plrs) do
            for i2,v2 in pairs(v:WaitForChild("PlayerGui"):GetChildren()) do
                if v2.Name == "NMP" then
                    v.Visible = true
                end
            end
        end
    end
end
    -- Rest of code

Anyways, I hope this helped. If not, or if you have any further problems/questions, please leave a comment below, and I'll see what I can do.

0
It fixed the problem, but now I have code that does "plr.PlayerGui..." though it is not in a for loop. What should I do now? Spoookd 32 — 8y
0
Could you upload that part of your code so I can see what you're talking about? dyler3 1510 — 8y

Answer this question