Find Rank by number in list but idk how

Hello everyone, I’m currently in the process of writing a module script that asks you to find the appropriate rank with the current XP number.

Here is an example of what my current list looks like:

local RankSystem = {}

local ranklist = {
	
	{key="rank1",value=50};
	{key="rank2",value=100};
	{key="rank3",value=200};
	{key="rank4",value=500};
	{key="rank5",value=1000};
}

function RankSystem.getRank(number)
	
-- IDK HOW TO GET THE RANK (KEY) BY THE NUMBER
	
end

return RankSystem

Let’s say the player has 356 XP and should get the rank “rank3” because it is lower than “rank4”.

How can I do this so that the module script finds out what rank the player has? The goal is to get the rank (as text) from the key so that the player can see what rank they have.

A Better way to store that RankList and do What you’re trying to achieve would be

local RankSystem = {}

local ranklist = {
	rank1 = 50,
	rank2 = 100,
	rank3 = 200,
	rank4 = 500,
	rank5 = 1000
}

function RankSystem.getRank(number)
	local MaxRank --> Store MaxRank So Far
	for RankName, RankValue in pairs(ranklist) do
		if number >= RankValue and RankValue > (ranklist[MaxRank] or 0) then
			MaxRank = RankName
		end
	end
	--> Once All of the Items have been Traversed, Get the MaxRank
	return MaxRank
end

return RankSystem

Yeah, but the ranks have names that are all different.

Here is an example of what it looks like in the original (yes it is in German):

{key="Polizeimeisteranwärter",value=0};
{key="Unterstützungskraft",value=50};
{key="Polizeimeister",value=100};
{key="Polizeiobermeister",value=200};
{key="Polizeihauptmeister",value=500};

Then just do

local ranklist = {
	
	rank1 = [key="rank1",value=50];
	rank2 = [key="rank2",value=100];
	rank3 = [key="rank3",value=200];
	rank4 = [key="rank4",value=500];
	rank5 = [key="rank5",value=1000];
}

function RankSystem.getRank(number)
	local MaxRank --> Store MaxRank So Far
	for RankName, RankValue in pairs(ranklist) do
		if number >= RankValue.value and RankValue.value > (ranklist[MaxRank] or 0) then
			MaxRank = RankValue.key
		end
	end
	--> Once All of the Items have been Traversed, Get the MaxRank
	return MaxRank
end


return RankSystem

Tried it out and definitely got the rank name as print feedback. But what surprises me is that I (have 998 XP) get rank4 (200) and not rank5 (500). I sent a photo as proof.

I’ve now found another solution, which means the entire system has been revised and is reminiscent of a level system. I’ll still leave this topic open. Because maybe there will be a solution for it after all. But at least for me it is no longer relevant.