Rank/Level Script Not Displaying Correct Rank for Players?

I have the following script in my game, a module script stored in ReplicatedStorage:

local rank = {}
local BadgeService = game:GetService("BadgeService")
local ranks = { --- ranks and their associated levels
	["President"] = {lvl=120, badge=2124579788, abbreviation = "PRES"},
	["Commander"] = {lvl=110, badge=2124512184, abbreviation = "COM"},
	["General"] = {lvl=100, badge=2124509861, abbreviation = "GEN"},
	["Colonel"] = {lvl=90, badge=2124509860, abbreviation = "COL"},
	["Major"] = {lvl=80, badge=2124509859, abbreviation = "MAJ"},
	["Captain"] = {lvl=70, badge=2124509858, abbreviation = "CPT"},
	["Lieutenant"] = {lvl=60, badge=2124509857, abbreviation = "LT"},
	["Staff Sergeant"] = {lvl=50, badge=2125772356, abbreviation = "SSGT"},
	["Sergeant"] = {lvl=40, badge=2124509856, abbreviation = "SGT"},
	["Corporal"] = {lvl=30, badge=2124509854, abbreviation = "CPL"},
	["Specialist"] = {lvl=20, badge=2125772350, abbreviation = "SPC"},
	["Private First Class"] = {lvl=10, badge=2124509853, abbreviation = "PFC"},
	["Private"] = {lvl=1, badge=198196955, abbreviation = "PVT"},
}
function rank.Check(player,level) --- this sees what rank someone is. We use it for the GUI
	local stats = player:WaitForChild("leaderstats")
	local levels = stats:WaitForChild("Level")
	level = levels.Value
	for rak,tabl in pairs(ranks) do
		if level >= tabl.lvl then
			print(player.Name.." is a "..rak.." at level "..level)
			return rak
		end
	end
end

I have this in ReplicatedStorage so the GUIs and serverscripts can call it to display a player’s rank both in said GUIs and their chat tag that other players see.

The issue is, this script seems to cap out at “Captain” and won’t display the correct rank for people level 80 and over:
https://gyazo.com/634ebcdd3822e72c5f3ee7163b5d6863

As you can see, I’m a level 109 yet it still says I’m a Captain when I should be a General.

Why is it doing this?

3 Likes

Alright, so I tested it out.

If I am not mistaken, the issue is because you are not limiting it from above.
Meaning, 2 or more conditions can occur at the same time:

lets say you’re Level 109.

On one hand, 109 is higher than 70[ so it’d print Captain, because it’s >= 70.]
On the other hand, 109 is higher than 100[ so it should print General, since it’s >=100]

What you could do, is as long as it’s not the first/last rank, make sure to add a < condition aswell.
For e.g:

if level >= 70 and level < 80 – range to be a captain

1 Like

I know [ and tested] that it works sometimes and prints the correct rank and level, but it still will print a wrong statement.

You’ll need to check that the level is in-between the range [minimum and maximum possibly lvls] to stay as the rank.

For each of those ranks [except for the last one], you’ll need to have a range,for e.g:

Private - [1,10]
Captain[70,80]

1 Like
local rank = {}
local BadgeService = game:GetService("BadgeService")
local ranks = { --- ranks and their associated levels
	["President"] = {lvl=120, maxlvl=129, badge=2124579788, abbreviation = "PRES"},
	["Commander"] = {lvl=110, maxlvl=119, badge=2124512184, abbreviation = "COM"},
	["General"] = {lvl=100, maxlvl=109, badge=2124509861, abbreviation = "GEN"},
	["Colonel"] = {lvl=90, maxlvl=99, badge=2124509860, abbreviation = "COL"},
	["Major"] = {lvl=80, maxlvl=89, badge=2124509859, abbreviation = "MAJ"},
	["Captain"] = {lvl=70, maxlvl=79, badge=2124509858, abbreviation = "CPT"},
	["Lieutenant"] = {lvl=60, maxlvl=69, badge=2124509857, abbreviation = "LT"},
	["Staff Sergeant"] = {lvl=50, maxlvl=59, badge=2125772356, abbreviation = "SSGT"},
	["Sergeant"] = {lvl=40, maxlvl=49, badge=2124509856, abbreviation = "SGT"},
	["Corporal"] = {lvl=30, maxlvl=39, badge=2124509854, abbreviation = "CPL"},
	["Specialist"] = {lvl=20, maxlvl=29, badge=2125772350, abbreviation = "SPC"},
	["Private First Class"] = {lvl=10, maxlvl=19, badge=2124509853, abbreviation = "PFC"},
	["Private"] = {lvl=1, maxlvl=9, badge=198196955, abbreviation = "PVT"},
}
function rank.Check(player,level) --- this sees what rank someone is. We use it for the GUI
	local stats = player:WaitForChild("leaderstats")
	local levels = stats:WaitForChild("Level")
	level = levels.Value
	for rak,tabl in pairs(ranks) do
		if level >= tabl.lvl and level <= tabl.maxlvl then
			print(player.Name.." is a "..rak.." at level "..level)
			return rak
		end
	end
end

Try this, adding max levels would help in deciding the rank, as currently like @Valkyrop says, you have no range, and it is just giving you a rank.

2 Likes

Good job,

but if he wants players to stay as President above 120, with no limit, then the last one don’t need a limit.

I assume he wants players to be ranked as President as long as their level is >=120

1 Like

Instead of “>=” I would use “if level < tabl.lvl” so you can avoid multiple statements being true.
*The only downside to this is it’ll cause an error if you don’t have an end Rank…I would create an end rank with level 9999 etc as a catch all.

2 Likes

To sum up :

local rank = {}
local BadgeService = game:GetService("BadgeService")
local ranks = { --- ranks and their associated levels
	["President"] = {lvl=120, badge=2124579788, abbreviation = "PRES"},
	["Commander"] = {lvl=110, maxlvl=119, badge=2124512184, abbreviation = "COM"},
	["General"] = {lvl=100, maxlvl=109, badge=2124509861, abbreviation = "GEN"},
	["Colonel"] = {lvl=90, maxlvl=99, badge=2124509860, abbreviation = "COL"},
	["Major"] = {lvl=80, maxlvl=89, badge=2124509859, abbreviation = "MAJ"},
	["Captain"] = {lvl=70, maxlvl=79, badge=2124509858, abbreviation = "CPT"},
	["Lieutenant"] = {lvl=60, maxlvl=69, badge=2124509857, abbreviation = "LT"},
	["Staff Sergeant"] = {lvl=50, maxlvl=59, badge=2125772356, abbreviation = "SSGT"},
	["Sergeant"] = {lvl=40, maxlvl=49, badge=2124509856, abbreviation = "SGT"},
	["Corporal"] = {lvl=30, maxlvl=39, badge=2124509854, abbreviation = "CPL"},
	["Specialist"] = {lvl=20, maxlvl=29, badge=2125772350, abbreviation = "SPC"},
	["Private First Class"] = {lvl=10, maxlvl=19, badge=2124509853, abbreviation = "PFC"},
	["Private"] = {lvl=1, maxlvl=9, badge=198196955, abbreviation = "PVT"},
}
function rank.Check(player,level) --- this sees what rank someone is. We use it for the GUI
	local stats = player:WaitForChild("leaderstats")
	local levels = stats:WaitForChild("Level")
	level = levels.Value
	for rak,tabl in pairs(ranks) do
		if tabl.maxlvl ~= nil then
			if level >= tabl.lvl and level <= tabl.maxlvl then
				print(player.Name.."-"..level.."-"..rak)
				return rak
			end
		else -- we dont want the last one to have a limit
			if level >= 120 then
				print(player.Name.."-"..level.."-President!!")
			end
		end
	end
end

return rank
1 Like

image

1 Like

What I ended up doing was setting the President’s max rank to math.huge

1 Like

Yes, that’d work aswell, glad you solved it!

2 Likes