 # Problems with table iteration

I’m trying to iterate through a table that contains a player’s exp. Using the function below, I’d like it to find the level for the player, then set it.

Function:
[lua]
function GetExpLevel(ply)

plyExp = ply:getDarkRPVar(“exp”)
–iterates through levels in table to find closest match, the current level
for k, v in ipairs(LEVELS) do

``````print("K = ".. k)
print("V = ".. v)

--if v &lt;= plyExp then
if v &lt;= tonumber(plyExp) then

print("V is less than or equal to plyExp")

if LEVELS[k+1] &gt; tonumber(plyExp) then

print("Setting Exp Level")
SetExpLevel(ply, k)

end

end

end
``````

end
[/lua]

Table
[lua]
local LEVELS = {}

LEVELS = 0
LEVELS = 100
LEVELS = 250
LEVELS = 400
LEVELS = 550
LEVELS = 700

LEVELS = 900
LEVELS = 1100
LEVELS = 1300
LEVELS = 1500
LEVELS = 1700

LEVELS = 2000
LEVELS = 2300
LEVELS = 2600
LEVELS = 2900
LEVELS = 3200

LEVELS = 3700
LEVELS = 4200
LEVELS = 4700
LEVELS = 5200
LEVELS = 5700
[/lua]

I print each key and value for testing purposes, and when the code runs, the players exp is at 68 (prints on console from different function) and the player’s level is nil, meaning the level is never set. Lastly, when running the function, the line with ‘print(“V is less than or equal to plyExp”)’ never executes.

Thanks for the help!

Try

[lua]local plyExp = tonumber(ply:getDarkRPVar(“exp”)) or 0[/lua]

``````

local LEVELS = {
100, 250, 400, 550, 700,
900, 1100, 1300, 1500, 1700,
2000, 2300, 2600, 2900, 3200,
3700, 4200, 4700, 5200, 5700,
}

function AdjustExpLevel(ply)
local val, temp = tonumber(ply:getDarkRPVar("exp")) or 0, 0
for i = 1, #LEVELS do
if LEVELS* < val then temp = i end
end
SetExpLevel(ply, temp)
end

``````

Works perfectly, thanks!

Sorry, I was mistaken, as the code snippet below only works for level 0. After that, the level stays to ‘0’ after earning the required exp for the next level.

Thanks again!

This snippet as well doesn’t work either. The same errors occur.

Thanks!

Either ply:getDarkRPVar(“exp”) doesn’t return correct value or it just “doesn’t work” because it sets next level when exp value is strictly greater than required, not greater or equal. If it’s the second then replace

``````

if LEVELS* < val then temp = i end

``````

with

``````

if LEVELS* <= val then temp = i end

``````

I’ve tried greater than or equal to before, and was testing without it. Every time I run the code, I have a testing function that prints every key and value from the table, as well as the player’s exp. A player’s exp is shown on the HUD correctly, and with every kill it increases by 15 correctly.

Thanks!

Wait so did that fix it or not? Also, please post you’re full code, I have no idea what you currently have.

No, it didn’t fix it. Same thing as the original code. I’ve tried mijyuoon’s code,

[lua]
local LEVELS = {
100, 250, 400, 550, 700,
900, 1100, 1300, 1500, 1700,
2000, 2300, 2600, 2900, 3200,
3700, 4200, 4700, 5200, 5700,
}

function AdjustExpLevel(ply)
local val, temp = tonumber(ply:getDarkRPVar(“exp”)) or 0, 0
for i = 1, #LEVELS do
if LEVELS* < val then temp = i end
end
SetExpLevel(ply, temp)
end
[/lua]

which makes the level at ‘0’, but nothing else.

I still can’t figure out whats wrong. I’m back at square one with the original code.

what is ply:getDarkRPVar(“exp”) returning?

A predefined value of 68. Once a player kills a bot/player, the exp will increase by 15 and return said value.

the code works fine for me, what exp are you testing it with? 68 is under 100, so 0 is what it should be returning

My issue is when it goes up past level 0, for instance once the exp hits 240, the return for level is 0.

if tonumber(ply:getDarkRPVar(“exp”)) is nil, it gets replaced with 0, are you sure it’s actually 68?

Yes, in a client side script I have the ‘exp’ and ‘level’ variables print out on HUD print hook.

Is there another way I could create a leveling system by obtaining a players Exp? I don’t want to use 30 else if statements, as that’s redundant and inefficient.

Thanks!

A mathematical formula, probably exponential.