Dzsozi (h03)

Get the range between two table keys

Recommended Posts

Hello!

So I am trying to make a level system with rank titles added to it. My only problem is that I can't figure out how can I get the range between the ranks.

More clearly:

local levelToRank = {
	[1] = {"Citizen", "Citizen"},
	[3] = {"Person of Interest", "Lookout"},
	[5] = {"Felon", ""},
	[10] = {"Reckless", ""},
	[15] = {"Hustler", "Good Guy"},
	[20] = {"Thug", ""},
	[25] = {"Fixer", ""},
	[30] = {"Soldier", ""},
	[40] = {"Gangster", ""},
	[50] = {"Trigger", "Meddler"},
	[60] = {"Associate", ""},
	[70] = {"Enforcer", ""},
	[75] = {"Immortal", ""},
	[80] = {"Organizer", ""},
	[85] = {"Lieutenant", ""},
	[90] = {"High Roller", ""},
	[100] = {"Public Enemy", ""},
	[110] = {"Shot Caller", ""},
	[120] = {"Rainmaker", ""},
	[130] = {"Right Hand", ""},
	[140] = {"Street Boss", ""},
	[150] = {"Kingpin", ""},
	[160] = {"Chapo", ""},
}

Here's my table with the rank names. I want to make it work like if I am level 1 and under level 3 then my returned rank name would be Citizen, but if I am level 3 but under level 5 then my rank would be displayed as Person of Interest and so on. So basically I would like to get the range between the levels. As you can see, the keys in the tables are the ranks where the name should be starting to display. So the range is the table key and the following table key, but I don't know how to properly get that and return the correct string. Please somebody help me out, it looks easy but I can't figure out the calculations and looping methods.

Edited by Dzsozi (h03)

Share this post


Link to post

Hello, I made function for you:
 

local levelToRank = {
	[1] = {"Citizen", "Citizen"},
	[3] = {"Person of Interest", "Lookout"},
	[5] = {"Felon", ""},
	[10] = {"Reckless", ""},
	[15] = {"Hustler", "Good Guy"},
	[20] = {"Thug", ""},
	[25] = {"Fixer", ""},
	[30] = {"Soldier", ""},
	[40] = {"Gangster", ""},
	[50] = {"Trigger", "Meddler"},
	[60] = {"Associate", ""},
	[70] = {"Enforcer", ""},
	[75] = {"Immortal", ""},
	[80] = {"Organizer", ""},
	[85] = {"Lieutenant", ""},
	[90] = {"High Roller", ""},
	[100] = {"Public Enemy", ""},
	[110] = {"Shot Caller", ""},
	[120] = {"Rainmaker", ""},
	[130] = {"Right Hand", ""},
	[140] = {"Street Boss", ""},
	[150] = {"Kingpin", ""},
	[160] = {"Chapo", ""},
}

function getRank ( rank )
	local _Rank = rank
	if( levelToRank[ _Rank ] )then
		return levelToRank[ _Rank ]
	end
	repeat 
		_Rank = _Rank - 1;
	until levelToRank[ _Rank ]
	return levelToRank[ _Rank ]
end
print( getRank( 155 )[1] ) -- Kingpin

 

Edited by iMr.WiFi..!
  • Like 1

Share this post


Link to post
57 minutes ago, iMr.WiFi..! said:

Hello, I made function for you:
 


local levelToRank = {
	[1] = {"Citizen", "Citizen"},
	[3] = {"Person of Interest", "Lookout"},
	[5] = {"Felon", ""},
	[10] = {"Reckless", ""},
	[15] = {"Hustler", "Good Guy"},
	[20] = {"Thug", ""},
	[25] = {"Fixer", ""},
	[30] = {"Soldier", ""},
	[40] = {"Gangster", ""},
	[50] = {"Trigger", "Meddler"},
	[60] = {"Associate", ""},
	[70] = {"Enforcer", ""},
	[75] = {"Immortal", ""},
	[80] = {"Organizer", ""},
	[85] = {"Lieutenant", ""},
	[90] = {"High Roller", ""},
	[100] = {"Public Enemy", ""},
	[110] = {"Shot Caller", ""},
	[120] = {"Rainmaker", ""},
	[130] = {"Right Hand", ""},
	[140] = {"Street Boss", ""},
	[150] = {"Kingpin", ""},
	[160] = {"Chapo", ""},
}

function getRank ( rank )
	local _Rank = rank
	if( levelToRank[ _Rank ] )then
		return levelToRank[ _Rank ]
	end
	repeat 
		_Rank = _Rank - 1;
	until levelToRank[ _Rank ]
	return levelToRank[ _Rank ]
end
print( getRank( 155 )[1] ) -- Kingpin

  

Thank you, it is kinda working, but not the way I expected and I want it to work. Here's my current script (I edited your code a little):

local levelToRank = {
	[1] = {"Citizen", "Citizen"},
	[3] = {"Person of Interest", "Lookout"},
	[5] = {"Felon", ""},
	[10] = {"Reckless", ""},
	[15] = {"Hustler", "Good Guy"},
	[20] = {"Thug", ""},
	[25] = {"Fixer", ""},
	[30] = {"Soldier", ""},
	[40] = {"Gangster", ""},
	[50] = {"Trigger", "Meddler"},
	[60] = {"Associate", ""},
	[70] = {"Enforcer", ""},
	[75] = {"Immortal", ""},
	[80] = {"Organizer", ""},
	[85] = {"Lieutenant", ""},
	[90] = {"High Roller", ""},
	[100] = {"Public Enemy", ""},
	[110] = {"Shot Caller", ""},
	[120] = {"Rainmaker", ""},
	[130] = {"Right Hand", ""},
	[140] = {"Street Boss", ""},
	[150] = {"Kingpin", ""},
	[160] = {"Chapo", ""},
}

function getPlayerTitleName(player)
	local playerLevel = getPlayerLevel(player)
	local rank = playerLevel
	
	if(levelToRank[rank])then
		return levelToRank[rank][1]
	end
	
	repeat 
		rank = rank + 1
	until levelToRank[rank]
	
	return levelToRank[rank][1]
end

But there is one problem. When I set my rank from 3 to for example 8 my title name is Person of Interest while it should be Felon, since 8 is above 5 and the Felon rank is from level 5. And it only changes when I set my level from 8 to 9, then it changes to Felon but that's no good for me. I always would like to get the correct rank name whenever I call the "getPlayerTitleName" function.

Edited by Dzsozi (h03)

Share this post


Link to post

 

2 minutes ago, iMr.WiFi..! said:

Line 36. You made it (+), It needs to be (-)

I changed it because if it is - and I am leveling up from 2 to 3 then I should already change rank to Person of Interest from Citizen, but this is not the case, only if I write + there. That's not the main problem, my main problem is if I am level 3 - Person of Interest and then I use /setlevel 150 for example, then the display still says Person of Interest and not Kingpin. So I mean it doesn't directly check the rank name for some reason.

Share this post


Link to post
1 minute ago, Dzsozi (h03) said:

 

I changed it because if it is - and I am leveling up from 2 to 3 then I should already change rank to Person of Interest from Citizen, but this is not the case, only if I write + there. That's not the main problem, my main problem is if I am level 3 - Person of Interest and then I use /setlevel 150 for example, then the display still says Person of Interest and not Kingpin. So I mean it doesn't directly check the rank name for some reason.

I don't think it a problem from function, You need to make it checks again when he sets a new level.

Share this post


Link to post

No, I don't think because as I said if I skip a rank name, the script still returns the following level's rank name. When I set my level from 8 (Felon) to 16 (Hustler) the script returns my rank name as Reckless which is between these two. It should return Hustler since I set my rank to 16.

Share this post


Link to post
Just now, Dzsozi (h03) said:

Works totally fine now, I also figured out that I also made a mistake. Thank you for your help guys!

Your welcome, good luck!

Edited by iMr.WiFi..!

Share this post


Link to post

I fixed it already, one problem was that I checked the wrong level when I used my custom event handler, but it's all working now fine!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.