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

getting all table values?

Asked by
Astralyst 389 Moderation Voter
6 years ago
Edited 6 years ago

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.

0
sorry for the very unorganized format Astralyst 389 — 6y
0
What you want the function to do? Because you can't change a player's name like that AvionicScript 65 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

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
Ad
Log in to vote
0
Answered by
zblox164 531 Moderation Voter
6 years ago

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!

Answer this question