How do I loop through the folder in a table backwards?

as it says in the title. I am wondering how to loop through the items in a folder backwards. So how do I do this? Cause I MUST KNOW HOW!!! Lol

You could get the table, make an empty table, put the instances in that table via a for i = num, num do loop and use that to loop through. I would imagine it could work like this:

local folder = path.to.folder

local children = folder:GetChildren()
local reversedChildren = {}

for i = #children, 1, -1 do
	table.insert(reversedChildren, children[i])
end

for k, v in pairs(reversedChildren) do
	-- do stuff
end

Or, if the items are in alphabetical order (which in most cases, they are), you could simply just do this:

local folder = path.to.folder
local children = folder:GetChildren()

table.sort(children, function(a, b) 
	return a > b
end)

for k, v in pairs(reversedChildren) do
	-- do stuff
end

Hopefully this helps!

1 Like

I forgot I made this post lol! Thanks for the help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.