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
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
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