Jacobob14 Posted February 27, 2014 Share Posted February 27, 2014 as I can calculate the percentage of exp needed to reach the next level addEventHandler("onClientResourceStart", resourceRoot, function() end ) addEventHandler("onClientRender", root, function() local lvl = (getElementData(getLocalPlayer(),"level")) local exp = (getElementData(getLocalPlayer(),"exp")) local text = "" local UP = tonumber ( getElementData ( localPlayer, "level" ) ) or 0 if ( UP == 1 ) then text = "600" elseif ( UP == 2 ) then -- el 2 significa el level text = "1000" elseif ( UP == 3 ) then text = "1500" elseif ( UP == 4 ) then text = "2500" elseif ( UP == 5 ) then text = "3000" elseif ( UP == 6 ) then text = "5000" elseif ( UP == 7 ) then text = "6000" elseif ( UP == 8 ) then text = "7000" elseif ( UP == 9 ) then text = "9000" elseif ( UP == 10 ) then text = "11000" elseif ( UP == 11 ) then text = "13500" elseif ( UP == 12 ) then text = "17000" elseif ( UP == 13 ) then text = "20500" elseif ( UP == 14 ) then text = "23000" elseif ( UP == 15 ) then text = "27000" elseif ( UP == 16 ) then text = " 31000" elseif ( UP == 17 ) then text = "34000" elseif ( UP == 18 ) then text = "38000" elseif ( UP == 19 ) then text = "42000" elseif ( UP == 20 ) then text = "47000" elseif ( UP == 21 ) then text = "52000" elseif ( UP == 22 ) then text = "58000" elseif ( UP == 23 ) then text = "64000" elseif ( UP == 24 ) then text = "70000" elseif ( UP == 25 ) then text = "75000" elseif ( UP == 26 ) then text = "80000" elseif ( UP == 27 ) then text = "85000" elseif ( UP == 28 ) then text = "90000" elseif ( UP == 29 ) then text = "95000" elseif ( UP == 30 ) then text = "100000" elseif ( UP == 31 ) then text = "105000" elseif ( UP == 32 ) then text = "110000" elseif ( UP == 33 ) then text = "115000" elseif ( UP == 34 ) then text = "125000" elseif ( UP == 35 ) then text = "135000" elseif ( UP == 36 ) then text = "145000" elseif ( UP == 37 ) then text = "160000" elseif ( UP == 38 ) then text = "175000" elseif ( UP == 39 ) then text = "190000" elseif ( UP == 40 ) then text = "220000" elseif ( UP == 41 ) then text = "240000" elseif ( UP == 42 ) then text = "260000" elseif ( UP == 43 ) then text = "280000" elseif ( UP == 44 ) then text = "300000" elseif ( UP == 45 ) then text = "320000" elseif ( UP == 46 ) then text = "340000" elseif ( UP == 47 ) then text = "360000" elseif ( UP == 48 ) then text = "380000" elseif ( UP == 49 ) then text = "400000" elseif ( UP == 50 ) then text = "420000" elseif ( UP == 51 ) then text = "440000" elseif ( UP == 52 ) then text = "460000" elseif ( UP == 53 ) then text = "480000" elseif ( UP == 54 ) then text = "500000" elseif ( UP == 55 ) then text = "520000" elseif ( UP == 56 ) then text = "540000" elseif ( UP == 57 ) then text = "560000" elseif ( UP == 58 ) then text = "580000" elseif ( UP == 59 ) then text = "600000" elseif ( UP == 60 ) then text = "620000" end dxDrawText("Nivel :", 216, 566, 267, 586, tocolor(0, 0, 0, 255), 0.70, "bankgothic", "left", "top", false, false, false, false) dxDrawText("Nivel :", 216, 567, 267, 586, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "left", "top", false, false, false, false) dxDrawText(lvl, 306, 564, 360, 596, tocolor(0, 0, 0, 255), 0.85, "bankgothic", "left", "top", false, false, false, false, false) dxDrawText(lvl, 307, 564, 360, 596, tocolor(255, 255, 255, 255), 0.85, "bankgothic", "left", "top", false, false, false, false, false) dxDrawText("Exp :", 366, 567, 410, 586, tocolor(0, 0, 0, 255), 0.70, "bankgothic", "left", "top", false, false, false, false, false) dxDrawText("Exp :", 367, 568, 410, 586, tocolor(255, 254, 254, 255), 0.70, "bankgothic", "left", "top", false, false, false, false, false) dxDrawText( ""..exp.." [ "..(text-exp).." ]", 428, 570, 494, 596, tocolor(0, 0, 0, 255), 0.65, "bankgothic", "left", "top",false,false,false,true,false ) dxDrawText( ""..exp.." [ "..(text-exp).." ]", 429, 570, 494, 596, tocolor(255, 255, 255, 255), 0.65, "bankgothic", "left", "top",false,false,false,true,false ) end ) addEventHandler ( "onClientResourceStart", resourceRoot, dxsetText ) Link to comment
myonlake Posted February 27, 2014 Share Posted February 27, 2014 (edited) It's just basic math, really. -- The current amount of experience local current_exp = 3000 -- The next level's experience local next_exp = 4000 -- Calculate the percentage till next level local percentage = math.ceil( 100 - ( current_exp / next_exp * 100 ) ) print( percentage ) -- Prints 25 Note, that you should extend the mathematical algorithm further, as at the moment you are not able to calculate more than one level with this code (it will start from 50 percent from level 2 on). Edited February 27, 2014 by Guest Link to comment
Jacobob14 Posted February 27, 2014 Author Share Posted February 27, 2014 I do not understand Link to comment
myonlake Posted February 27, 2014 Share Posted February 27, 2014 Use the mathematical algorithm inside the print function, for example like the following: -- The current amount of experience the player has local current_experience = 0 -- Ascending order with numerical keys containing the experience points for each level local experience_levels = { 100, 250, 500, 1000, 1500, 2000, 2750, 3250, 4000, 5000, 6250, 7500, 8750, 10000 } -- A function to get the level from experience points local function getLevelFromExperience( experience ) if ( type( experience ) ~= "number" ) then return false, 1 end -- Loop through all experience levels for level,_experience in ipairs( experience_levels ) do -- If there is another level after the current level if ( experience_levels[ level + 1 ] ) then -- If the current experience is less than the next level's experience if ( experience < experience_levels[ level + 1 ] ) then -- Return this level return level end else -- If no other level was found, then return the current level return level end end return false, 2 end -- A function to calculate and return the percentage to next level local function getPercentageToNextLevel( experience ) if ( type( experience ) ~= "number" ) then return false, 1 end -- Check if the current experience doesn't exceed the maximum experience points if ( experience ~= experience_levels[ #experience_levels ] ) then -- Get the current level local level = getLevelFromExperience( experience ) or 1 -- Get the amount of experience required till next level local experience_to_level = experience - experience_levels[ level ] -- Reset the next level experience properly local fixed_experience = experience_levels[ level + 1 ] - experience_levels[ level ] -- Calculate and return the percentage between the levels return math.ceil( 100 - ( experience_to_level / fixed_experience * 100 ) ) end return 0 end -- Get the current width and height of the screen local screen_width, screen_height = guiGetScreenSize( ) -- Initialize a renderer to draw the level data on screen addEventHandler( "onClientRender", resourceRoot, function( ) -- Get the current level local level = getLevelFromExperience( current_experience ) or 1 -- If the experience is less than the maximum amount, then increment the current experience for demonstration if ( current_experience ~= experience_levels[ #experience_levels ] ) then current_experience = current_experience + 2 * level end -- Initialize a string containing the data local text = "Current level: " .. level .. " | Current experience: " .. current_experience .. " (Progress till next level: " .. getPercentageToNextLevel( current_experience ) .. "%)" -- Draw a shadow under the actual text dxDrawText( text, screen_width - dxGetTextWidth( text, 2.0 ) - 14, screen_height - dxGetFontHeight( 2.0 ) - 14, screen_width, screen_height, tocolor( 0, 0, 0, 125 ), 2.0, "default", "left", "top" ) -- Draw the actual text containing the level data dxDrawText( text, screen_width - dxGetTextWidth( text, 2.0 ) - 15, screen_height - dxGetFontHeight( 2.0 ) - 15, screen_width, screen_height, tocolor( 255, 255, 255, 225 ), 2.0, "default", "left", "top" ) end ) Note that the code is client-side as it has a renderer, but you can use the rest of the code server-side as there are no limitations there. I have not tested the text rendering however as I cannot try it right now, but the math and everything works as I tried it on the Lua engine. I also extended the mathematical algorithm so, that there is no issues with the levels no longer. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now