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

Help with iterating through a table?

Asked by 8 years ago
local admins = {'Player1', 'Player2'}

for _, v in ipairs(admins) do
    print(_, admins)
    if player.Name == v then
        adminButton.Visible = true
    else 
        break
    end
end

When it prints, it prints some random letters and number in the output. I'm trying to go through the lists of admins, but it only seems to work for whoever is first in the table.

0
what's adminButton, you never defined it theCJarmy7 1293 — 8y
0
That doesn't matter in the code. The error is not the adminbutton, it's it not looping through the admins NinjoOnline 1146 — 8y

1 answer

Log in to vote
6
Answered by
Link150 1355 Badge of Merit Moderation Voter
8 years ago

It prints random numbers because you are printing the "underscore" variable. "underscore" in your script is given the position of 'v' in the table 'admins'.

Also, your script only works for the first person in the table because you stop the loop using 'break' if player.Name is not equal to v (that is, the first value in the table: "Player1").

0
The break only occurs if the players name is not the admin. The game only had 2 players, Player1 and Player2. As for it printing random numbers could you explain more?? NinjoOnline 1146 — 8y
0
Sorry, the website kinda screwed the formatting of my text. You are printing the value of the 'underscore' variable, which is given the position of 'v' in the table you are browsing. Link150 1355 — 8y
0
Also, a little correction: the break only occurs if the player's name is not the first in the admin table. Remove the "else break end" and everything will be fine. Link150 1355 — 8y
0
Thank you! works now. Is there anyway I can like break it, cause I don't want it looping forever, when it only needs to check when the player has joined NinjoOnline 1146 — 8y
0
Your loop won't loop forever. It only loops once for each element in your table, comparing the current element to the player's name. Link150 1355 — 8y
Ad

Answer this question