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

How to reverse the order of a list?

So, I am trying to use a list in a project I am making, however the list is the wrong way round. I need to reverse the list order. I have already tried:

local amount = #tools
	local toollist = {}
	while amount >= 1 do
		table.insert(toollist,tools[amount].Name)
		amount = amount - 1
	end

Also, the amount of index's can change, it could be any amount.

2 comments

0

So everything in the list is backwards?

0

Yes

1 answer

Try this!

local origTable = {"Hello","I'm","Goulstem"}; --The original table
local newTable = {}; --The one you will reverse the original one into

function reverse(old,new)
	for i = 1,#old do --For every index the old one has
		-- Make a new index in the new table. 
		-- Set it as the last index in the old table - 'i'
		-- Subtract another 1 since 'i' starts at 1 and you want every index
		new[#new+1] = old[#old-(i-1)];
	end
end

reverse(origTable,newTable);
print(unpack(newTable)) --> Goulstem I'm Hello

1 comment

0

You cannot tell how many indexes there will be, it changes.

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