Finding next value in table

I’m making a game with a ton ranks, and I need some help with a script.

I want to make a for loop to go through all the ranks and see if the player has reached that rank. How could I check if their leaderstats XP is higher than Rank1 but lower than Rank2?

All the ranks are listed in a modulescript in the following format:

local Ranks = { --Rank = {GroupRankId, XpNeeded}
	Rank1 = {1, 0},
	Rank2 = {2, 50},
	Rank3 = {3, 200},
}

return Ranks
1 Like

May I ask why there are 2 numbers instead of one?

If your talking about the {1, 0} and {2, 50},the first number is the group rank id, and the second number is the amount of XP required to get to that rank.

1 Like

To access the rank you can go around doing something like this I think:

local Ranks = { --Rank = {GroupRankId, XpNeeded}
	Rank1 = {1, 0},
	Rank2 = {2, 50},
	Rank3 = {3, 200},
}

-- get xp needed
Ranks[1][2]
-- get group rank id
Ranks[1][1]

I know how to do that. My question is if I am doing a for loop going through the ranks, how could get get Rank3 (the next rank) while the for loop is on Rank2?

pretty sure you can use the next()

local XP = 40
local Rank
for i,v in pairs(Ranks) do
if XP >= Ranks[v][2] then
Rank = Ranks[v][1]
else
print(“Not enough XP”)
end
end

2 Likes

Could I do next(Ranks, i+1)?

1 Like

Is there a reason why ranks aren’t set in an array? Will you be adding other info?
Otherwise this seems sufficient.

local ranks = {0, 50, 200}

for rank, amount in ipairs(ranks) do
   local thisRankAmount = amount
   local thisRank = rank
   if not rank == #ranks then
       local nextRankAmount = ranks[rank+1]
   
   else
      --Reached max rank
   end
end
2 Likes

I originally planned on adding in the rank name, but then i realized I could just get it from the rank Id. I am also thinking about adding rank images in the future.

1 Like

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