[HUD STUFF] Xp Bar Help!

Hello guys im right now trying to make a hud with having the xp bar so i can have a leveling system that i am working.



if SERVER then


elseif CLIENT then

local LEVELS = {}
LEVELS[1] = 0
LEVELS[2] = 50
LEVELS[3] = 250
LEVELS[4] = 400
LEVELS[5] = 650
LEVELS[6] = 850
LEVELS[7] = 1060
LEVELS[8] = 1475
LEVELS[9] = 1655
LEVELS[10] = 2235
LEVELS[11] = 2600
LEVELS[12] = 3450
LEVELS[13] = 3800
LEVELS[14] = 4250
LEVELS[15] = 4753
LEVELS[16] = 5127
LEVELS[17] = 5476
LEVELS[18] = 6000
LEVELS[19] = 6345
LEVELS[20] = 7150
LEVELS[21] = 8546
LEVELS[22] = 10563
LEVELS[23] = 11245
LEVELS[24] = 12085
LEVELS[25] = 12965
LEVELS[26] = 13035
LEVELS[27] = 13646 
LEVELS[28] = 14703
LEVELS[29] = 15235
LEVELS[30] = 15860
LEVELS[31] = 16605
LEVELS[32] = 17859
LEVELS[33] = 18002
LEVELS[34] = 18864
LEVELS[35] = 19930
LEVELS[36] = 20104
LEVELS[37] = 23102
LEVELS[38] = 25704
LEVELS[39] = 27777
LEVELS[40] = 28643
LEVELS[41] = 30100
LEVELS[42] = 32500
LEVELS[43] = 34708
LEVELS[44] = 36114
LEVELS[45] = 39141
LEVELS[46] = 40411
LEVELS[47] = 43411
LEVELS[48] = 47141
LEVELS[49] = 49141
LEVELS[50] = 53114
function myhud()
//xp bar
local xpx = ScrW() - 850
local xpy = ScrH() - 150

	local client = LocalPlayer()
	if !client:Alive() then return end
	if(client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end
	draw.RoundedBox(3, xpx, xpy, 600, 50, Color(51, 58, 51, 255)) // Bar background
	if xp >= LEVELS[xp] then
		draw.RoundedBox(3, xpx, xpy, math.Clamp( xp, 0, 300)*2, 50, Color(0, 255, 0, 255)) //Xp Bar
	end
	
end
hook.Add("HUDPaint", "myhud", myhud)


end


I didn’t Show the other stuff its mainly just the xp bar is not working right it won’t goto the end and then do my sound each i already have setup it works good
and it doesn’t restart which i understand that i didn’t put that in but right now the main issue is when i goto level 3 its like
Example:
Xp Bar LEVEL: 3

||||||||||||||||||||||||||||||||||||||||||

its basicly not finshed to hit 3 i don’t know how to fix that if you can help Thanks!

What does xp variable equal to?

LEVELS[What ever level] = xp amount
im trying to grab from LEVELS[blah] = HERE!!!
and grab what ever number there



	if xp >= LEVELS[xp] then

Except for xp = 1, xp can never be bigger than or equal LEVELS[xp].
If xp is bigger than 50 you will get an error.

Well its not given me any errors and plus its little bit weird how its not listen to what it is…

This is what i am using in my level system:



local curlvl = {}

local exp = self:GetNWInt( 'EXP' )
		
for k, v in pairs( levels ) do
		
	if exp >= v then
			
		table.insert( curlvl, k )
				
	end
			
end

local lvl = table.maxn( curlvl )


In your case, i would think:



local curlvl = {}
		
for k, v in pairs( LEVELS ) do
		
	if xp >= v then
			
		table.insert( curlvl, k )
				
	end
			
end

local lvl = table.maxn( curlvl )  -- This will be what your current level is. You could now set the level as a NWInt or something of the sort.


I think it has to do with the xp bar it self else anyone has a better idea

[editline]23rd March 2014[/editline]

well… could i change that to a hud?

[editline]23rd March 2014[/editline]

because its a bit of hud i think mainly else i put some extra code to the myhud()

[editline]23rd March 2014[/editline]

so i could probably put the hud in right? like this…



local curlvl = {}
		
for k, v in pairs( LEVELS ) do
		
	if xp >= v then
			
		table.insert( curlvl, k )
	        		draw.RoundedBox(3, xpx, xpy, math.Clamp( xp, 0, 300)*2, 50, Color(0, 255, 0, 255))

	end
			
end

local lvl = table.maxn( curlvl )


Don’t do it like that
Why would you store all the levels in a table and then select the biggest one?
Just have it like that:



local maxlvl = 0

local exp = self:GetNWInt( 'EXP' )
		
for k, v in pairs( levels ) do
		
	if exp >= v && k > maxlvl then
		maxlvl = k;
	end
			
end

Or even better, have a bijective function, get the inverse function for that and pass the exp to that inverse function.
for example the inverse of f(x) = x^2 is f(y) = sqrt(y) (for non negative y values)

@xrayhunter1:
Your code is just completely broken, you need a seperate variable for the xp and the level you have, you can use the way TheEncrypted showed (or better the improved one) to calculate
the current level of the player. Then you use that as the key to the LEVELS table and then do the drawing.

Oh my bad, i thought you were having problems determining the level from the amount of experience you currently have. For drawing the HUD you are going to need to find the amount of experience required for the next level and compare that to the experience that you currently have. I will see if i can find some example code for you.

could i do



local xpcur = {}

local xp = ply:GetNWInt("xp")

for k, v in pairs( xpcur ) do
       if xp >= v && k > xpcur then 
                		draw.RoundedBox(3, xpx, xpy, math.Clamp( xpcur, 0, 300)*2, 50, Color(0, 255, 0, 255))
end


NoNoNo, you have the wrong idea here. Since i thought you wanted to find the level from the amount
of experience that the player has, i gave you code to do exactly that ( even thought sly0r’s was much
better and also much cleaner ). If you want to draw the xp bar, you are going to need to compare the
next level experience requirement and the experience that you currently have, like i said in the previous
post.

I havn’t read any of the thread aside from your code and i have a tip or another method you could use to make your levels. I dont know what would be a good interval but why not make the levels go up at like 1.4 * the previous level. So lets say level[1] = 150 and you did a for loop 49 times and add to that table for each key = i and each value = (level* - 1) * 1.4 or your interval. Idk just some brainstorming. Maybe ill actually write the code instead of explaining it.

But to answer your question. You would need to use a HUDPaint hook and you could just do it outside the cl_hud.lua file for the gamemode. Assuming your asking how you would clamp it. You could just set the min to 0 and the max to the previous level xp - the current level xp and have a counter showing your exact xp.

I talk too much…

[editline]23rd March 2014[/editline]



levels = {}
levels[1] = 100
	for i=1, 49 do
		table.insert(levels,i+1,math.Round((levels*)*1.2))
	end
PrintTable(levels)


Just to take away the big line of "level[2] = 150, level[3] = 250, etc.