1
  ```  local xpNeed = 100; -- E.g. 100 * lvl = XP you need to rank up!
    addEvent("onPlayerLevelUp", true);

    function addPlayerXp(player, xp)
        local acc = getPlayerAccount(player);
        local oldexp = getAccountData(acc, "exp") or 0;
        local oldlvl = getAccountData(acc, "lvl") or 1;
        local newlevel = oldlvl + 1;
        local newexp = oldexp + xp;
        lvl = getAccountData(acc, "lvl") or 0;
        if getElementData(player,'lvl') > 9 then return end
        setAccountData(acc, "exp", newexp);
        setElementData(player, "exp", newexp); 
        --if getAccountData(acc,'lvl') > 9 then return end
            if (newexp >= (oldlvl * xpNeed)) then
            local expleft = newexp - (oldlvl * xpNeed); -- added calculation for the exp that may remains after level up.
            outputChatBox("[Level-Up] Congratulations! New level "..newlevel.."!", player, 66, 134, 244);
            setAccountData(acc, "lvl", newlevel);
            setAccountData(acc, "exp", expleft);
            setElementData(player, "exp", expleft);
            setElementData(player, "lvl", newlevel);
            triggerEvent("onPlayerLevelUp", player, newlevel, oldlvl, oldexp, newexp);
        end
    end
    addEventHandler("onPlayerLogin", root, function()
        local acc = getPlayerAccount(source);
        if acc then
            setElementData(source, "lvl", getAccountData(acc, "lvl") or 0);
            setElementData(source, "exp", getAccountData(acc, "exp") or 0);
        end
    end);

    addEventHandler("onZombieGetsKilled", root, function(killer)
        if killer and getElementType(killer) == "player" then
            addPlayerXp(killer, 50);
        end
    end);

    addEventHandler("kilLDayZPlayer", root, function(killer)
        if (killer and killer ~= source and getElementType(killer) == "player") then
            addPlayerXp(killer, 50);
        end
    end);

    --[[
    addEventHandler("onPlayerSpawn", root, function(newlvl)
        if newlvl then
        if (lvl) >= 1 then
                setElementData(player, "MP5A5", 1)
            elseif lvl >= 2 then
                setElementData(player, "Tent", 1)
            elseif lvl >= 3 then
                setElementData(player, "Milk", 1)
            elseif lvl >= 4 then
                setElementData(player, "Medic Kit", 1)
            elseif lvl >= 5 then
                setElementData(player, "Shovel", 1)
            end
        end
    end);

    ]]

    table = {"Milk","MP5A5","M4A1-S","AK-47","Soda Bottle","Pizza","AS50","Tent","Medium Tent","ACR","AR-15","M107","Pasta Can","Beans Can","Golf Club"}

    addEventHandler("onPlayerSpawn", root, function(lvl)
        if not lvl then lvl = getElementData(player, "lvl") or 0; end
        if lvl >= 1 then
            for i = 1, lvl do
                setElementData(player, table[i], getElementData(player, table[i]) + 1);
            end
        end
    end);
```

Hello, this is my first post here, so i'd like to say hello. So, as a beginner programmer, i am trying to implement a level system for my mta server, but, i've met an obstacle as stated in the title. Where's this boolean value? How could i make it work? I'm sorry if this question seems trivial (as it surely does), but i really want to move forward. Any help will be appreciated Thanks in advance!

error is in setElementData(player, table[i], getElementData(player, table[i]) + 1); what can i do to make it work any help will be appreciated

ERROR LINE 70 attempt to perform arithmetic on boolean value AND WARRNING SAME LINE

Bad argument @ getElementData [ Expected element at argument 1, got nil ]

LuAmateur
  • 11
  • 3

3 Answers3

0

getElementData can return false if the element you asked for doesn't exist. I assume you want that to be treated as 0, so replace getElementData(player, table[i]) + 1 with (getElementData(player, table[i]) or 0) + 1.

Also, it looks like you don't have a player variable there, so it's using a nil global, so you'll always get nil. To fix that, do local player = source at the beginning of your onPlayerSpawn handler.

0

So i use your functions and my code looks like that

   table = {"Milk","MP5A5","M4A1-S","AK-47","Soda Bottle","Pizza","AS50","Tent","Medium Tent","ACR","AR-15","M107","Pasta Can","Beans Can","Golf Club"} 

addEventHandler("onPlayerSpawn", root, function(lvl)
local player = source 
    if not lvl then lvl = getElementData(player, "lvl") or 0; end
    if lvl >= 1 then
        for i = 1, lvl do
            setElementData(player, "lvl", table[i], (getElementData(player,"lvl", table[i]) or 0) + 1);
        end
    end
end);

i add "lvl" bcs prnt.sc/sc9hk1 i got but stil not working and now i have this error : https://prnt.sc/scajbc

LuAmateur
  • 11
  • 3
0

Firstly, please don't use table as a variable name. I haven't written for Multi Theft Auto but I'm guessing it has the Lua builtin table library, which you'll be overwriting.

getElementData returns false if the data doesn't exist for that element, so you need to check to make sure it does.

What your code is doing is, on each player spawn is increasing the number of that item by one, I'm assuming what you actually want to do is just give the player one of that item?

Eitherway, something like this:

 addEventHandler("onPlayerSpawn", root, function(lvl)
        if not lvl then lvl = getElementData(player, "lvl") or 0; end
        if lvl >= 1 then
            for i = 1, lvl do
                local itemCount = getElementData(player, table[i]) or 0
                setElementData(player, table[i], itemCount + 1);
            end
        end
    end);

If you're just trying to add a single item on each spawn and not increase it by 1, then do this setElementData(player, table[i], 1);

Jack
  • 1
  • 2