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
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 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
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.