Let's say, I have a table.
local player1 = {"username", "username2", "etc"}
Player.Name == player1[1] or player1[2] or payer1[3] or player1[4] or player1[5]
How could I let's say do
Player.Name == player1[all]
instead of repeating player[1]/player[#] and so on?
I tried a for i,v; didn't work.
I tried doing
for i=1, #player1 do print(player1[i]) end
but it only goes through one player, then stops.
If you want to get all table values then use a for loop
local table = {1,3,4,5,6,5,56,7,7} -- Can contain anything for i,v in pairs (table) do --[[ do whatever you want i = the place of the object being used. if i is 2 then it's meaning the 2nd element v = is the element, like if it's dealing with the 2nd element of the table then v is 3 and i 2 --]] end
Simple! You just use the pairs()
loop or ipairs()
loop.
There are two loops for doing this. Pairs
and ipairs
. The main difference is ipairs
will break if you have a nil value in the table.
local Table1 = {1, 2, 3, "NIL", nil, 5, 6, 9, "Randomstuff"} for index, value in pairs(Table1) do print(value) -- This would output every value in the table end for index, value in ipairs(Table1) do print(value) -- This would output every value up to the nil value end
Thats how to loop around tables and what pairs
and ipairs
do.
Hope this helps!