Adding a rank a player already has

Ranks module

return {
	Peasant		= {Color = Color3.fromRGB(150, 150, 150), Level = 1},
	Knight		= {Color = Color3.fromRGB(71, 169, 255), Level = 10, Cost = 500},

Script

for i, v in pairs(Ranks) do
		if v.Level then
			if User.Level >= v.Level then
				for _, userRanks in pairs(User.Ranks) do
					if i ~= userRanks then
						table.insert(User.Ranks, i)
						print(1)
						break
					end
				end
			end
		end
	end

So the idea behind this is, whenever I update a players level, i run the above loop to go through all the Ranks inside the Rank module and see if the player has passed any milestone levels, so they can unlock a new rank. However, when a player hits Level 10, the print(1) prints twice and ends up adding Knight and Peasant to User.Ranks. It should just add the 1 new rank and then stop there. I can’t use return as I have further code below this. My thought process was that

if i ~= userRanks then

would mean if they didn’t have the rank then add it. The player automatically starts off with Peasant rank in their Ranks table, so it should never be added twice

I would solve it like this:

local userRank;

for num, rank in pairs(Ranks) do
    if rank.Level then
        if (User.Level >= rank.Level) and (rank.Level > userRank.Level) then
            userRank = rank;
        end
    end
end

-- Their highest rank is now "userRank"
print(userRank.Level)
Working code and results

3 Likes

What’s userRank?? I just have a Ranks table, like so

local User = {Ranks = {'Peasants'}}

and then I just want to add in ‘Knight’ to that table when they hit the specific level. There’s more ranks to for different levels, so needs to be able to work with a dozen ranks, not just 2 (which I’m pretty sure it should work anyway :grimacing:)

But ye, not sure what userRank and thus userRank.Level is in this situation

userRank in this case is the highest achieved rank the user has. If you see under the “Working code and results” you can see that when I print userRank and userRank.Level it shows the highest rank in the RANKS table.

I chose a bad wording for this situation, my bad! I could have gone with userHighestRank etc.

Here is probably a better example, with comments and stuff.

local Ranks = {
    Peasant = {Level = 1},
    Knight = {Level = 10},
    King = {Level = 50},
    Dragon = {Level = 100}
};
Updated code
local UserHighestRank = nil;
local User = {Level = 12};

for rankName, rank in pairs (Ranks) do
    if rank.Level then
        
        -- Checking if the user's level is matching this rank's level
        -- and if this rank's level is higher than "UserHighestRank" 's level
        -- or if it is nil
        
       if ((User.Level >= rank.Level) and (UserHighestRank == nil or UserHighestRank.Level < rank.Level)) then
           -- Setting this as the temporary highest rank, so we can check with further ranks
           UserHighestRank = rank;
       end
    end
end

-- Now that the loop is done, we know this is their Highest Rank!
print("User's highest rank:", UserHighestRank.Level)

When user's level is 5

image
image

When user's level is 49

image
image

When user's level is 101

image
image

1 Like