Getting index from folder not accurate

I have this system for player auras and what should happen is that the aura folder with the next number should be selected but once the aura number goes past 11 it seems to not be right and goes in the wrong order.

Folder of all the auras:
Screenshot 2024-10-11 at 18.29.55

ServerScript:

remotes.auraSettingEvent.Event:Connect(function(player: Player, direction: string)
    local lastWornAura = Manager.GetLastAuraWorn(player)
	local playerRank = Manager.GetRank(player)

	if table.find(debounceTable2, player) then return end
	table.insert(debounceTable2, player)
	task.delay(0.5, function()
		table.remove(debounceTable2, debounceTable[player])
	end)

	local auraIndex = getAuraIndex(lastWornAura)
	if direction == "foreward" then
		auraIndex += 1
	elseif direction == "back" then
		auraIndex -= 1
	end

	local aura = auraParticles:GetChildren()[auraIndex]
	if not aura then return end
	
    print(auraIndex)
    print(aura)

	for _, rank in ipairs(Ranks) do
		if rank.Name ~= playerRank.Name then continue end

		local rankIndex = table.find(Ranks, rank)
		local rankNeededToUnlock = string.gsub(aura.Name, "%D", "")

		if tonumber(rankNeededToUnlock) > rankIndex then return end
	end

	Manager.SetLastAuraWorn(player, aura)
	Manager.AdjustSetting(player, "CurrentAura", auraIndex)
	remotes.settingsRemote:FireClient(player, "CurrentAura", auraIndex)

	updateAura(player, aura, lastWornAura)
end)
1 Like

Use a table.sort() and string.match() for reorder a table

local Example = {
    "3 Pie",
    "1 Apple",
    "4 Tomato",
    "2 Pineapple",
    "5 Coconut"
}

table.sort(Example, function(A, B)
    
    if typeof(A) == "string" and typeof(B) == "string" then
        local Number1 = tonumber(string.match(A, "%d+"))
        local Number2 = tonumber(string.match(B, "%d+"))
        
        if Number1 and Number2 then
            return Number1 < Number2
        end
    end
end)

print(Example)  -- Output: {"1 Apple", "2 Pineapple", "3 Pie", "4 Tomato", "5 Coconut"}
2 Likes

I try that that I get this in the output:

[1] = 1 Frail Auras,
[2] = 15 F Class Auras,
[3] = 12 Beginner Class Auras,
[4] = 14 F- Class Auras,
[5] = 17 C Class Auras,
[6] = 16 C- Class Auras,
[7] = 18 B- Class Auras,
[8] = 13 Promising Class Auras,
[9] = 10 Learner Class Auras,
[10] = 2 Average Class Auras,
[11] = 11 Rookie Class Auras,
[12] = 5 Newbie Class Auras,
[13] = 9 Trainee Class Auras,
[14] = 6 Starter Class Auras,
[15] = 19 B Class Auras

It seems to be even worse

local Example = {}

for _, v in ipairs(auraParticles:GetChildren()) do
	table.insert(Example, v)
end

table.sort(Example, function(A, B)
	if typeof(A) == "string" and typeof(B) == "string" then
		local Number1 = tonumber(string.match(A, "%d+"))
		local Number2 = tonumber(string.match(B, "%d+"))

		if Number1 and Number2 then
			return Number1 < Number2
		end
	end
end)

print(Example)

Remove the typeof conditional check and change A and B to A.Name and B.Name.

It still doesn’t print in order.

my bad

local Example = {
    [1] = 1 hello world,
    [2] = 3 bar,
    [3] = 2 foo,
    [5] = 4 foo,
    [6] = 5 foo,
    [4] = 6 foo,
}

local List = {}

for _, Child in pairs(Example) do

    if typeof(Child) == "Instance" then
        table.insert(List, Child.Name)
    else
        if typeof(Child) == "string" then
            table.insert(List, Child)
        end
    end
end

table.sort(List, function(A, B)
    
    if typeof(A) == "string" and typeof(B) == "string" then
        local A_Digit = tonumber( string.match(A, "%d+") )
        local B_Digit = tonumber( string.match(B, "%d+") )
        
        if A_Digit and B_Digit then
            return A_Digit < B_Digit
        else
            return A < B
        end
    end
end)

print(List)