Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

1 comment

0

@ iiFirestorm Yes to your question.

2 answers

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!

3 comments

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?

0

@ iiFirestorm Yes.

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)

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


4 comments

0

should work for a loop

0

Thanks for your support, but it didn't work.

0

really? worked for me. I made this on my phone by the way, so I'll edit my answer soon

0

That one works fine for me.

This discussion is preserved as it was. New answers, comments, and votes are closed.