Need help looping around a number

I don’t know if the title is misleading

So I have a ammo system within my game but I need help making the UI actually work

    local startNum = ammoData.currentMag
	local currNum = startNum - 1
	
	repeat
		local UI = hud.Ammo.Main:FindFirstChild("Spare"..currNum)
		
		if UI then
			local sortOrder = startNum - currNum --My whole problem in this line
			
			UI.LayoutOrder = sortOrder
		end
		
		currNum += 1
		if currNum > #ammoData.spare then
			currNum = 0
		end
	until currNum == startNum
    -- Rest of the code is just resizing

What I want the end result to look like
image
The starting bar moves all the way to the end and the second bar becomes the first bar

What it looks like currently
image
Instead of moving the bar to the end, it just continues

P.S I can’t keep it like this because the player isn’t going to only have 5 magazines at a time, sometimes more, sometimes less

my goofy ahh brain just did a a simpler way
the code if you’re stealing my code somehow

    local startNum = ammoData.currentMag
	local currNum = startNum
	
	local sortOrder = 0
	
	repeat
		local UI = hud.Ammo.Main:FindFirstChild("Spare"..currNum)
		
		if UI then
			UI.LayoutOrder = sortOrder
			sortOrder += 1 --I dunno why I didn't do this earlier
		end
		
		currNum += 1
		if currNum > #ammoData.spare then
			currNum = 0
		end
	until currNum == startNum