Rank system is nearly working

Hello this is a rank system i made, it’s nearly working but the ranks aren’t ordered well as shown in the dictionary when i print, and maybe thats the reason for it, how could i fix this?

function updateRank(Char,Plr)
	local Ranks = {
		[5] = "Normal",
		[9] = "Experienced",
		[15] = "Pro",
		[19] = "Epic",
		[25] = "Legendary",
		[30] = "Semi-God",
		[50] = "God",
		[95] = "The Best",
		[125] = "Titanium",
		[150] = "Unreachable Rank"
	}

	if Char and Plr then
		local Gui = Char.Head.RankGui
		for NumberForRank, Rank in pairs(Ranks) do
			if Plr.leaderstats.Strength.Value >= NumberForRank then
				print(Ranks[NumberForRank])
				Gui.Rank.Text = Ranks[NumberForRank]
			end
		end
	end
end

For pairs, just use the Rank value instead of trying to index it

function updateRank(Char,Plr)
	local Ranks = {
		[5] = "Normal",
		[9] = "Experienced",
		[15] = "Pro",
		[19] = "Epic",
		[25] = "Legendary",
		[30] = "Semi-God",
		[50] = "God",
		[95] = "The Best",
		[125] = "Titanium",
		[150] = "Unreachable Rank"
	}

	if Char and Plr then
		local Gui = Char.Head.RankGui
		for NumberForRank, Rank in pairs(Ranks) do
			if Plr.leaderstats.Strength.Value >= NumberForRank then
				print(Rank)
				Gui.Rank.Text = Rank -- just the value
			end
		end
	end
end

I have 126 strength, and its supposed to give the titanium rank but it doesn’t?

What rank does it set you with

It gives me “The best” rank, i just printed the rank value, and it printed all the ranks and “The best” rank its last

Dictionaries aren’t ordered by design in Lua.
Create an array of this and stores two values instead:

local Ranks = {{5, "Normal"}, {9, "Experienced"}, {15, "Pro"}, {19, "Epic"}, {25, "Legendary"}, {30, "Semi-God"}, {50, "God"}, {95, "The Best"}, {125, "Titanium"}, {150, "Unreachable Rank"}}
if Char and Plr then
    local Gui = Char.Head.RankGui
    for _, Rank in ipairs(Rank) do
        if Plr.leaderstats.Strength.Value >= Rank[1] then
            print(Rank[2])
            Gui.Rank.Text = Rank[2]
        end
    end
end
3 Likes

Ok, I just did some tests and came up with this

function updateRank(Char,Plr)
	local Ranks = { -- instead of indexing values with an integer, just set a table with the rank and number
		{"Normal", 5},
		{"Experienced", 9},
		{"Pro", 15},
		{"Epic", 19},
		{"Legendary", 25},
		{"Semi-God", 30},
		{"God", 50},
		{"The Best", 95},
		{"Titanium", 125},
		{"Unreachable Rank", 150}
	}

	if Char and Plr then
	    local Gui = Char.Head.RankGui
		for NumberForRank, Rank in pairs(Ranks) do
			if Plr.leaderstats.Strength.Value >= Rank[2] then -- Rank[2] is the int that is needed
				print(Rank[1]) -- Rank[1] is the rank
                Char.Rank.Text = Rank[1]
			end
		end
	end
end
2 Likes