How to reverse the i number in a loop

I’m making a UIListLayout, but I want the most recent item to be listed at the bottom, and I’ve found that the highest number in a layout will be displayed at the bottom. Using a script, how could I get e.g. i = 1 to equal i = 3 (the highest number possible in the list).

4 Likes

You can make it count down by doing this:

local HighestNumber = 3
local LowestNumber = 1
for i = HighestNumber,LowestNumber,-1 do
	print(i)
end
2 Likes

Or reverse the output:

local highest = 3
for i = 1, highest do
	local reversed = highest-i+1 --+1 because lua is a funny language
	print(reversed)
end
1 Like

How could I incorporate this in the loop I’m using to create the items?

for i, message in pairs({messageLog[#messageLog], messageLog[#messageLog - 1], messageLog[#messageLog - 2]}) do
		createObject("TextLabel", {
			BackgroundTransparency = 1,
			Parent = logFrame,
			Size = UDim2.new(1, 0, 0, 50),
			Font = Enum.Font.GothamBold,
			TextColor3 = Color3.new(1, 1, 1),
			TextXAlignment = "Left",
			TextScaled = true,
			Text = message,
			TextTransparency = 0,
			TextWrapped = true,
			ZIndex = 2,
			Name = i,
			createObject("UIStroke", {
				Color = Color3.new(0, 0, 0),
				Thickness = 2,
				Transparency = 0,
			})
		})
	end

The loop grabs the last three logs and like I said I’m trying to get the most recently added one at the bottom, with the highest number so the UIListLayout puts it at the bottom.

local amount = 3 
for i = 1, amount do
	local reversed = #messageLog-i+1
	local message = messageLog[reversed]
	--run create object here
	--and do Name = reversed
end

Also,

local amount = 3
for i = #messageLog, #messageLog-amount, -1 do
	local message = messageLog[i]
	--same with above but Name = i
end
1 Like

Works really well! Just gotta make some tweaks and this will be perfect!

1 Like

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