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.

1local player1 = {"username", "username2", "etc"}
1Player.Name == player1[1] or  player1[2] or payer1[3] or player1[4] or player1[5]

How could I let's say do

1Player.Name == player1[all]

instead of repeating player[1]/player[#] and so on?

I tried a for i,v; didn't work.

I tried doing

1for i=1, #player1 do
2print(player1[i])
3end

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

1local table = {1,3,4,5,6,5,56,7,7} -- Can contain anything
2 
3for i,v in pairs (table) do
4    --[[
5        do whatever you want
6        i = the place of the object being used. if i is 2 then it's meaning the 2nd element
7        v = is the element, like if it's dealing with the 2nd element of the table then v is 3 and i 2
8    --]]
9end
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.

1local Table1 = {1, 2, 3, "NIL", nil, 5, 6, 9, "Randomstuff"}
2 
3for index, value in pairs(Table1) do
4    print(value) -- This would output every value in the table
5end
6 
7for index, value in ipairs(Table1) do
8    print(value) -- This would output every value up to the nil value
9end

Thats how to loop around tables and what pairs and ipairs do. Hope this helps!

Answer this question