Get table entry from value

In my game, I have ranks that require a certain amount of XP to reach. How can I figure out what rank the player on join by comparing their XP saved in a datastore to the XP required for each rank?

The ranks are sorted in a table like this:

local ranks = {0, 50, 200} --Rank1 XP, Rank2 XP, Rank3 XP, etc
local rankXpRequirements = {0, 50, 200}

function findEligibleRank(player)
    for rank, xpReq in rankXpRequirements do
        if xpReq > player.Xp then 
            return rank - 1 --Req too high, so the highest eligible rank is the previous one
        end
    end
    return #rankXpRequirements --Max rank
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.