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

Is there any way to reverse the order of a table?

For example, if I had this TableSet={}

And then I inserted a few items in there

Players=game.Players:GetChildren()
for i=1,#Players do
table.insert(TableSet,Players[i].Name)
end

and made a visual list of the table

for _,string in pairs(TableSet) do
local Cont = Instance.new("TextLabel",scrollGUI) -- assuming scrollGUI already exists
Con.Name = "Content: "..Num
Con.Position = UDim2.new(0,4,0,NumPos)
Con.Size = UDim2.new(1,0,0,30)
Con.Text = String
end

If the table was in this order: Player1 Player2 Player3

How would I reverse it, making it so the content would appear like this from top-bottom?: Player3 Player2 Player1

I have literally no clue how to do this, any help would be appreciated, thank you!

1 answer

There does not seem to be an official method according to the table manipulation.

You can use the non required second property of table.insert which allows you to set the position, and set a second parameter to 1.

Players=game.Players:GetChildren()
for i=1,#Players do
table.insert(TableSet,1,Players[i].Name)
end

Your other option is to make the loop go backwards through the list using a third parameter in a for loop that sets the increment.

for i=#TableSet, 1, -1 do --So the index is set to the last part of the TableSet and the loop will continue to go until it reaches 1, which can be accomplished from starting off at 22 or something by subtracting with the increment.
local Cont = Instance.new("TextLabel",scrollGUI) -- assuming scrollGUI already exists
Con.Name = "Content: "..Num
Con.Position = UDim2.new(0,4,0,NumPos)
Con.Size = UDim2.new(1,0,0,30)
Con.Text = TableSet[i] --i is the index value of TableSet, so the script knows that what you want is in whatever place i is in.
end --End to end the loop.

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