For example:
local Ranks = {
["Starter"] = {
["LevelRequired"] = 0,
},
["Noob"] = {
["LevelRequired"] = 5,
},
["Strong"] = {
["LevelRequired"] = 10,
},
["Expert"] = {
["LevelRequired"] = 15,
},
}
I don’t want to manually check if the player’s level is between 2 Level Requireds.
How can I effectively get the player’s rank just by knowing their level?
Example of what I don’t want to be doing:
if Level >= Ranks.Starter.LevelRequired and Level < Ranks.Starter.LevelRequired then
-- is Starter rank
end
if Level >= Ranks.Noob.LevelRequired and Level < Ranks.Strong.LevelRequired then
-- is Noob rank
end
-- And so on
Thank you for help 
Let me know if you didn’t understand or if you have a question on what I’m trying to achieve!
3 Likes
-- Your dictionary needs to be converted to a iteratable table first. We will encode the name as the first element and put the rest of the info as another table.
-- This will let you add more information to each rank but still let us programmatically access the info we need.
-- We also need to ensure they are sorted from lowest to highest rank -- very important!!!
-- You could move this at the top of the function but it doesn't really need to run every time
local RanksArray = {}
for k,v in pairs(Ranks) do
table.insert(RanksArray, {
name = k,
info = v
})
end
table.sort(RanksArray, function(a, b)
if a.info.LevelRequired < b.info.LevelRequired then
return true
end
end)
function GetRankFromLevel(level)
local rank = RanksArray[1]; -- Start at the lowest rank
for k, v in ipairs(RanksArray) do
if v.info.LevelRequired <= level and level > rank.info.LevelRequired then
rank = v
end
end
return rank.name
end
print(GetRankFromLevel(12)) -- Expected Output: "Strong"
2 Likes
Nevermind I found out how. Someone helped me in HiddenDevs
thanks for helping me though!
2 Likes