``` 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);
```
[你好,这是我在这里的第一篇文章,所以我想打个招呼。因此,作为一个初学者程序员,我正在尝试为我的MTA服务器实现一个级别系统,但是,我遇到了标题中所述的障碍。这个布尔值在哪里?我该如何运作?很抱歉,这个问题似乎微不足道(确实如此),但是我真的很想前进。任何帮助将不胜感激,谢谢!
错误发生在setElementData(player,table [i],getElementData(player,table [i])+ 1);我该怎么做才能使其发挥任何帮助,将不胜感激
错误70行尝试对布尔值执行算术运算和警告同一行
错误参数@ getElementData [参数1处的期望元素,为零]
getElementData
可以返回false。我假设您希望将其视为0,所以将getElementData(player, table[i]) + 1
替换为(getElementData(player, table[i]) or 0) + 1
。
而且,看起来您那里没有player
变量,因此它使用的是nil全局变量,因此您总是会得到nil。要解决此问题,请在local player = source
处理程序的开头执行onPlayerSpawn
。
所以我使用了您的函数,我的代码看起来像这样
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);
我添加了“ lvl” bcs prnt.sc/sc9hk1我得到了,但stil无法正常工作现在我有这个错误:https://prnt.sc/scajbc