 Help making code efficient and simplifying XP and levelling

Hi. Recently i have been trying to implement XP and levelling into the gamemode i am creating. I am able to assign the player their level as well as give them XP for doing damage to enemies which is currently damage done divided by two.

Basically the code works well however it is not very efficient meaning i will have to write hundreds of lines of code for each level doing it this way.

Does anyone have and ideas on how i can simplify this process and make the code more efficient?

Here is the xp values for each level

lvl1 = 0
lvl2 = 83
lvl3 = 174
lvl4 = 276
lvl5 = 388
lvl6 = 512
lvl7 = 650
lvl8 = 801
lvl9 = 969
lvl10 = 1154
lvl11 = 1358
lvl12 = 1584
lvl13 = 1833
lvl14 = 2107
lvl15 = 2411
lvl16 = 2746
lvl17 = 3115
lvl18 = 3523
lvl19 = 3973
lvl20 = 4470
lvl21 = 5018
lvl22 = 5624
lvl23 = 6291
lvl24 = 7028
lvl25 = 7842
lvl26 = 8740
lvl27 = 9730
lvl28 = 10824
lvl29 = 12031
lvl30 = 13363

This code is then using these values and checking them against their XP and tells me if they are above the level.
If i was to do this for every level as well as assign the player the actual level too. It would require a lot of lines of code. Is there a simpler way to do this?

local currentxp = self:getgunxp()
self:setgunxp( currentxp + amount )
end

function meta:setgunxp(amount)
self:SetNetworkedInt( "gunxp", amount )
self:savexp()
end

function meta:getgunxp()
return self:GetNetworkedInt("gunxp")

end

function meta:checkxp()
local currentxp = self:getgunxp()
if currentxp > lvl1 then self:ChatPrint("above level 1") elseif currentxp > lvl20 then self:ChatPrint("above level 20")  end
end

function meta:savexp()
local xp = self:getgunxp()
self:SetPData("gunxp", xp)
end

Try something like this
[LUA]
local levels = {
 = 0,
 = 83,
 = 174,
 = 276,
 = 388,
 = 512,
 = 650,
 = 801,
 = 969,
 = 1154,
 = 1358,
 = 1584,
 = 1833,
 = 2107,
 = 2411,
 = 2746,
 = 3115,
 = 3523,
 = 3973,
 = 4470,
 = 5018,
 = 5624,
 = 6291,
 = 7028,
 = 7842,
 = 8740,
 = 9730,
 = 10824,
 = 12031,
 = 13363
}

function meta:checkxp()
local currentxp = self:getgunxp()
local lastLevel = 0
for k,v in pairs(levels) do
if currentxp > v then
lastLevel = k
else
break
end
end

self:ChatPrint("above level "..lastLevel)

end
[/LUA]

This looks promising. I’ll give it a go, thank you very much.

[editline]5th August 2016[/editline]

This looks promising. I’ll give it a go, thank you very much.

[editline]5th August 2016[/editline]

Works perfectly Thanks!

https://i.gyazo.com/b2b460be0db118a76885141af6212fc5.png

[editline]5th August 2016[/editline]

Also advice you should use Get/SetNWInt instead of Get/SetNetworkedInt because Get/SetNetworkedInt is deprecated

You’re better off using a formula to calculate the experience.

I found the best model for your leveling up experience to be:

f(x)=(x*3.77)^2

You can see Desmos thinks so too:

http://puu.sh/qrhtb.png

Your next step would be to create the function as a Lua function, something along the lines of…

function getLevelExperience( level )
return ( level * 3.77 ) ^ 2
end

function getLevelBounds( level )
-- min, max
return getLevelExperience( level ), getLevelExperience( level + 1 )
end