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
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
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.
1 comment
Sorry about that, try it now. I forgot to edit.