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

[SOLVED] How can I call the last two values of a table?

Asked by 9 years ago

I fixed it on my own, but thanks for the support. None of the answers really helped so I made my own workaround that works perfect.

0
@ iiFirestorm Yes to your question. EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

Yes, that is easy. To call something in a table you need to do something like this:

mytable = {1,2,3,4,5,6,7,8,9,1337, "Roblox"}

print(mytable[10]) --Should print "1337".

Now to call the last thing in the table do this:

mytable = {1,2,3,4,5,6,7,8,9,1337, "Roblox"}

print(mytable[#mytable]) --Print the 11th one since there are only 11 of them.

To do the second to last one do this:

mytable = {1,2,3,4,5,6,7,8,9,1337, "Roblox"}

print(mytable[(#mytable-1)]) --Should print "1337", 11-1= 10 which is the 2nd to last one on the table.

There, that is all!

0
Let's say the table is constantly being added to, new numbers each time at the end, and I want to print() the last two every time. Only one would be added at a time. How would I call them THEN, same method? iiFirestorm 5 — 9y
0
@ iiFirestorm Yes. EzraNehemiah_TF2 3552 — 9y
0
Still problems. It doesn't seem to find the second to last, but does find the last. table.insert(fs, table.maxn(fs)+#fs-1) iiFirestorm 5 — 9y
Ad
Log in to vote
0
Answered by 9 years ago
local tab = {0,1,2}

for i = 1,10,1 do
table.insert(tab,1,i)
local last = tab[#tab] --#table returns the length of the table as an integer
local secondlast = tab[#tab - 1]
local first = tab[1]
print(last, secondlast, first)
wait(1)
end


0
should work for a loop aquathorn321 858 — 9y
0
Thanks for your support, but it didn't work. iiFirestorm 5 — 9y
0
really? worked for me. I made this on my phone by the way, so I'll edit my answer soon aquathorn321 858 — 9y
0
That one works fine for me. aquathorn321 858 — 9y

Answer this question