noob scripter but i want an admin script to only be visible to the players that are inside the table by accessing the Players, finding their names and putting a gui in their PlayerGui, i used the in pairs loop because thats the best way i know to do it, but i got nothing in the output and nothing works. (i am testing to see if it works by making it print "found", instead i get nothing)
01 | local admins = { "PicoTheRiko" , "toxa_pas" } |
02 |
03 | local player = game.Players:GetChildren() |
04 |
05 | for i, v in pairs (player) do |
06 | if v.Name = = admins then |
07 | print ( "found" ) |
08 | else |
09 | print ( "not" ) |
10 | end |
11 | end |
So, you were on the right track by using a pairs loop, but you weren't checking each entry of your "admins" table in it.
"if v.Name == admins then"
That line will try to compare each name to the table itself, which of course won't work for what you're trying to do.
Here's how I would implement it:
01 | local Players = game:GetService( "Players" ) |
02 | local adminList = { "PicoTheRiko" , "toxa_pas" } -- I'd recommend using player's UserId's, as opposed to using their Username, because if they change their name then you'll have to update this list. |
03 |
04 | local function checkForAdmins() |
05 | local players = Players:GetPlayers() |
06 | for _, Player in pairs (players) do |
07 | local foundAdmin = false ; -- used to track whether or not this Player is an admin |
08 | for _, admin in pairs (adminList) do |
09 | if Player.Name = = admin then |
10 | foundAdmin = true |
11 | break -- breaks the admin loop |
12 | end |
13 | end |
14 | if foundAdmin then -- checks to see if the previous loop changed the foundAdmin bool at all |
15 | print (Player.Name .. " is an admin." ) |
I think it's because you're not referrencing the table position, wich can be done like so:
1 | admins [ 1 ] -- position 1 wich is "PicoTheRiko" |
2 | admins [ 2 ] -- position 2 wich is "toxa_pas" |
So with that, we can make your script to print
01 | local admins = { "PicoTheRiko" , "toxa_pas" } |
02 |
03 | local player = game.Players:GetChildren() |
04 |
05 | for i, v in pairs (player) do |
06 | if v.Name = = admins [ 1 ] or v.Name = = admins [ 2 ] then |
07 | print ( "found" ) |
08 | else |
09 | print ( "not" ) |
10 | end |
11 | end |
Now, to make the gui appear, we can do it like so:
1 | local admins = { "PicoTheRiko" , "toxa_pas" } |
2 |
3 | local player = game.Players:GetChildren() |
4 |
5 | for i, v in pairs (player) do |
6 | if v.Name = = admins [ 1 ] or v.Name = = admins [ 2 ] then |
7 | game.Players.LocalPlayer:WaitForChild( "screenGuiNameHere" ).Enabled = true |
8 | end |
9 | end |
Test it out, if it still doesn't work, you can do a loop with a wait() to run that script every few seconds.