• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Weapon Upgrade lua script error

Lopaskurwa

Active Member
Joined
Oct 6, 2017
Messages
873
Solutions
2
Reaction score
49
Using this script
Action - [TFS 1.1] Item's upgrading by jewels
I know that i need to install this Action - Item's upgrading by jewels ver. 3 [FOR TFS 0.3.6]
but its for 0.3.6 not a TFS 1.2
Untitled.png
 
Solution
Okay, it's not actually a fix, as it works, you just failed to register the functions. Will go step by step on how I did it, do it like this.

1) Create a file called "jewel_upgrading" on data folder and paste this inside:
Code:
function getItemAttribute(uid, key)
   local i = ItemType(Item(uid):getId())
   local string_attributes = {
     [ITEM_ATTRIBUTE_NAME] = i:getName(),
     [ITEM_ATTRIBUTE_ARTICLE] = i:getArticle(),
     [ITEM_ATTRIBUTE_PLURALNAME] = i:getPluralName(),
     ["name"] = i:getName(),
     ["article"] = i:getArticle(),
     ["pluralname"] = i:getPluralName()
   }

   local numeric_attributes = {
     [ITEM_ATTRIBUTE_WEIGHT] = i:getWeight(),
     [ITEM_ATTRIBUTE_ATTACK] = i:getAttack(),
     [ITEM_ATTRIBUTE_DEFENSE] = i:getDefense(),
     [ITEM_ATTRIBUTE_EXTRADEFENSE] = i:getExtraDefense(),
     [ITEM_ATTRIBUTE_ARMOR] = i:getArmor(),
     [ITEM_ATTRIBUTE_HITCHANCE] = i:getHitChance(),
     [ITEM_ATTRIBUTE_SHOOTRANGE] =...
Code:
--Item Upgrading released by granaria.hopto.org for TFS 0.3.6
--Another guy made this script(TFS 0.3.5), I don't know who it was. I'll not take credit for this script whatsoever.

local config = {
    brokeChance = 5,
    upgradePercent = 8,
    maxUpgrade = 50,
    notAllowed = {}
}
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local getUpgraded = itemEx.actionid - 10000
    if getUpgraded < 0 then
        getUpgraded = 0
    end
    if getUpgraded < config.maxUpgrade then
        if itemEx.itemid > 100 then
            if isItemMoveable(itemEx.itemid) and not isItemRune(itemEx.itemid) and not isCorpse(itemEx.uid) and not isContainer(itemEx.uid)and not isItemStackable(itemEx.itemid) and not isItemFluidContainer(itemEx.itemid) and not isInArray(config.notAllowed, itemEx.itemid) then
                if config.brokeChance <= math.random(100) then
                    doItemSetAttribute(itemEx.uid, "name", getItemNameById(itemEx.itemid) .. "+" .. (getUpgraded+1))
                    doItemSetAttribute(itemEx.uid, "actionid", 10000+(getUpgraded+1))
                    doSendMagicEffect(toPosition, 30)
                    doPlayerSendTextMessage(cid, 22, "You successfuly upgraded " .. getItemNameById(itemEx.itemid) .. " to " .. (getUpgraded+1) .. " level.")
                    if getItemAttribute(itemEx.uid, "attack") and getItemAttribute(itemEx.uid, "attack") > 0 then
                        doItemSetAttribute(itemEx.uid, "attack", (getItemAttribute(itemEx.uid, "attack")+((getItemAttribute(itemEx.uid, "attack")/100)*config.upgradePercent))+1)
                    end       
                    if getItemAttribute(itemEx.uid, "extraattack") and getItemAttribute(itemEx.uid, "extraattack") > 0 then
                        doItemSetAttribute(itemEx.uid, "extraattack", (getExtraItemAttack(itemEx.uid)+((getExtraItemAttack(itemEx.uid)/100)*config.upgradePercent))+1)
                    end           
                    if getItemAttribute(itemEx.uid, "defense") and getItemAttribute(itemEx.uid, "defense") > 0 then
                        doItemSetAttribute(itemEx.uid, "defense", (itemEx.uid), (getItemAttribute(itemEx.uid, "defense")+((getItemAttribute(itemEx.uid, "defense")/100)*config.upgradePercent))+1)
                    end           
                    if getItemAttribute(itemEx.uid, "extradefense") and getItemAttribute(itemEx.uid, "extradefense") > 0 then
                        doItemSetAttribute(itemEx.uid, "extradefense", (itemEx.uid), (getItemAttribute(itemEx.uid, "extradefense")+((getItemAttribute(itemEx.uid, "extradefense")/100)*config.upgradePercent))+1)
                    end       
                    if getItemAttribute(itemEx.uid, "armor") and getItemAttribute(itemEx.uid, "armor") > 0 then
                        doItemSetAttribute(itemEx.uid, "armor", (itemEx.uid), (getItemAttribute(itemEx.uid, "armor")+((getItemAttribute(itemEx.uid, "armor")/100)*config.upgradePercent))+1)
                    end       
                    if getItemAttribute(itemEx.uid, "attackspeed") and getItemAttribute(itemEx.uid, "attackspeed") > 0 then
                        doItemSetAttribute(itemEx.uid, "attackspeed", (itemEx.uid), (getItemAttribute(itemEx.uid, "attackspeed")+((getItemAttribute(itemEx.uid, "attackspeed")/100)*config.upgradePercent))+1)
                    end       
                    if getItemAttribute(itemEx.uid, "hitchance") and getItemAttribute(itemEx.uid, "hitchance") > 0 then
                        doItemSetAttribute(itemEx.uid, "hitchance", (itemEx.uid), (getItemAttribute(itemEx.uid, "hitchance")+((getItemAttribute(itemEx.uid, "hitchance")/100)*config.upgradePercent))+1)
                    end
                else
                    doPlayerSendTextMessage(cid, 22, getItemNameById(itemEx.itemid).." upgrading to level " .. (getUpgraded+1) .. " fail! You lose item!")
                    doRemoveItem(itemEx.uid, 1)
                    doSendMagicEffect(toPosition, 34)
                end
                doRemoveItem(item.uid, 1)
            else
                doPlayerSendCancel(cid, "You cannot upgrade this item!")
            end
        end
    else
        doPlayerSendCancel(cid, "This item is already upgraded to maximum item level.")
    end
    return true
end
getItemAttribute returns nil if the attribute isn't present, so you first have to check if it returned true (not nil or false), and then compare it with a number ;)

Cykotitan replied to this post: Upgrade Items for TFS 0.3.6 asking a similar if not the same question.
Hopefully that works for you.
 
You are using 1.2 or 0.3.6, not clear to me
If you are indeed using 1.2, doItemSetAttribute will not work on it, as I think that function is no longer named like that, and, it isn't on default compat.lua
 
Last edited:
I don't have a server to test this so bear with me, try adding this function on data/lib/compat/compat.lua
Code:
function doItemSetAttribute(uid, attribute, value)
    local item = Item(uid)
    if item == nil then
        return false
    end

    item:setAttribute(attribute, value)
    return true
end
Add it at the very end, and post your results
 
I don't have a server to test this so bear with me, try adding this function on data/lib/compat/compat.lua
Code:
function doItemSetAttribute(uid, attribute, value)
    local item = Item(uid)
    if item == nil then
        return false
    end

    item:setAttribute(attribute, value)
    return true
end
Add it at the very end, and post your results
Sure no problem.
Untitled.png
 
Hmm..It seems it can't find a value for this line:
Code:
local nLevel = (conf["level"][(level+1)].successPercent >= math.random(1,100)) and (level+1) or conf["level"][level].downgradeLevel

Try replacing that line with:
Code:
print('Item Level: '..level)
local nLevel = (conf["level"][(level+1)].successPercent >= math.random(1,100)) and (level+1) or conf["level"][level].downgradeLevel

It will give you the same error code, but in console just before it, it would print "Item Level: X"
Post it, so we can find if it's receiving a nil value
----------
EDIT:
Don't worry mate, I will download a 1.2 server right now and test this whole script, if I fix it, I will post it here, stay tuned
 
Last edited:
Okay, it's not actually a fix, as it works, you just failed to register the functions. Will go step by step on how I did it, do it like this.

1) Create a file called "jewel_upgrading" on data folder and paste this inside:
Code:
function getItemAttribute(uid, key)
   local i = ItemType(Item(uid):getId())
   local string_attributes = {
     [ITEM_ATTRIBUTE_NAME] = i:getName(),
     [ITEM_ATTRIBUTE_ARTICLE] = i:getArticle(),
     [ITEM_ATTRIBUTE_PLURALNAME] = i:getPluralName(),
     ["name"] = i:getName(),
     ["article"] = i:getArticle(),
     ["pluralname"] = i:getPluralName()
   }

   local numeric_attributes = {
     [ITEM_ATTRIBUTE_WEIGHT] = i:getWeight(),
     [ITEM_ATTRIBUTE_ATTACK] = i:getAttack(),
     [ITEM_ATTRIBUTE_DEFENSE] = i:getDefense(),
     [ITEM_ATTRIBUTE_EXTRADEFENSE] = i:getExtraDefense(),
     [ITEM_ATTRIBUTE_ARMOR] = i:getArmor(),
     [ITEM_ATTRIBUTE_HITCHANCE] = i:getHitChance(),
     [ITEM_ATTRIBUTE_SHOOTRANGE] = i:getShootRange(),
     ["weight"] = i:getWeight(),
     ["attack"] = i:getAttack(),
     ["defense"] = i:getDefense(),
     ["extradefense"] = i:getExtraDefense(),
     ["armor"] = i:getArmor(),
     ["hitchance"] = i:getHitChance(),
     ["shootrange"] = i:getShootRange()
   }
  
   local attr = Item(uid):getAttribute(key)
   if tonumber(attr) then
     if numeric_attributes[key] then
       return attr ~= 0 and attr or numeric_attributes[key]
     end
   else
     if string_attributes[key] then
       if attr == "" then
         return string_attributes[key]
       end
     end
   end
return attr
end

function doItemSetAttribute(uid, key, value)
   return Item(uid):setAttribute(key, value)
end

function doItemEraseAttribute(uid, key)
   return Item(uid):removeAttribute(key)
end

2) Open data/global.lua and at the very top add this:
Code:
dofile('data/jewel_upgrading.lua')

3) Register the itemid for upgrading on actions.xml (You already have this done, I write it just in case)
Code:
<action itemid="XXXX" script="ZZZZ.lua"/> --XXXX for the itemid, ZZZZ for the script path

4) Inside the script for upgrading in data/actions/scripts, paste the original code, this one:
Code:
local conf = {
   ["level"] = {
   -- [item_level] = {successPercent= CHANCE TO UPGRADE ITEM, downgradeLevel = ITEM GETS THIS LEVEL IF UPGRADE FAILS}
     [1] = {successPercent = 85, downgradeLevel = 0},
     [2] = {successPercent = 80, downgradeLevel = 1},
     [3] = {successPercent = 75, downgradeLevel = 2},
     [4] = {successPercent = 70, downgradeLevel = 3},
     [5] = {successPercent = 65, downgradeLevel = 4},
     [6] = {successPercent = 60, downgradeLevel = 5},
     [7] = {successPercent = 55, downgradeLevel = 0},
     [8] = {successPercent = 50, downgradeLevel = 0},
     [9] = {successPercent = 45, downgradeLevel = 0}
   },

   ["upgrade"] = { -- how many percent attributes are rised?
     attack = 5, -- attack %
     defense = 5, -- defense %
     extraDefense = 10, -- extra defense %
     armor = 5, -- armor %
     hitChance = 5, -- hit chance %
   }
}



-- // do not touch // --
-- Upgrading system by Azi [Ersiu] --
-- Edited for TFS 1.1 by Zbizu --

local upgrading = {
  upValue = function (value, level, percent)
  if value < 0 then return 0 end
     if level == 0 then return value end
     local nVal = value
     for i = 1, level do
       nVal = nVal + (math.ceil((nVal/100*percent)))
     end
  return nVal > 0 and nVal or value
  end,

  getLevel = function (item)
  local name = Item(item):getName():split('+')
     if (#name == 1) then
       return 0
     end

     return math.abs(name[2])
  end,
}
function onUse(cid, item, fromPosition, itemEx, toPosition)
   local it = ItemType(itemEx.itemid)
  if((it:getWeaponType() > 0 or getItemAttribute(itemEx.uid, ITEM_ATTRIBUTE_ARMOR) > 0) and not isItemStackable(itemEx.itemid))then
  local level = upgrading.getLevel(itemEx.uid)
  if(level < #conf["level"])then
  local nLevel = (conf["level"][(level+1)].successPercent >= math.random(1,100)) and (level+1) or conf["level"][level].downgradeLevel
  if(nLevel > level)then
  doSendMagicEffect(toPosition, CONST_ME_MAGIC_GREEN)
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Upgrade to level " .. nLevel .. " successful!")
  else
  doSendMagicEffect(toPosition, CONST_ME_BLOCKHIT)
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Upgrade failed. Your " .. it:getName() .. " is now on level " .. nLevel .. "")
  end
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_NAME, it:getName()..((nLevel>0) and "+"..nLevel or ""))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_ATTACK,  upgrading.upValue(it:getAttack(), nLevel, conf["upgrade"].attack))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_DEFENSE, upgrading.upValue(it:getDefense(), nLevel, conf["upgrade"].defense))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_EXTRADEFENSE, upgrading.upValue(it:getExtraDefense(), nLevel, conf["upgrade"].extraDefense))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_ARMOR, upgrading.upValue(it:getArmor(), nLevel, conf["upgrade"].armor))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_HITCHANCE, upgrading.upValue(it:getHitChance(), nLevel, conf["upgrade"].hitChance))
  doRemoveItem(item.uid, 1)
  else
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Your " .. it:getName() .. " is on max level alredy.")
  end
  else
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You cannot upgrade this item.")
  end
end

5) Remove the functions I made you add to compat.lua

6) Enjoy.
 
Solution
Okay, it's not actually a fix, as it works, you just failed to register the functions. Will go step by step on how I did it, do it like this.

1) Create a file called "jewel_upgrading" on data folder and paste this inside:
Code:
function getItemAttribute(uid, key)
   local i = ItemType(Item(uid):getId())
   local string_attributes = {
     [ITEM_ATTRIBUTE_NAME] = i:getName(),
     [ITEM_ATTRIBUTE_ARTICLE] = i:getArticle(),
     [ITEM_ATTRIBUTE_PLURALNAME] = i:getPluralName(),
     ["name"] = i:getName(),
     ["article"] = i:getArticle(),
     ["pluralname"] = i:getPluralName()
   }

   local numeric_attributes = {
     [ITEM_ATTRIBUTE_WEIGHT] = i:getWeight(),
     [ITEM_ATTRIBUTE_ATTACK] = i:getAttack(),
     [ITEM_ATTRIBUTE_DEFENSE] = i:getDefense(),
     [ITEM_ATTRIBUTE_EXTRADEFENSE] = i:getExtraDefense(),
     [ITEM_ATTRIBUTE_ARMOR] = i:getArmor(),
     [ITEM_ATTRIBUTE_HITCHANCE] = i:getHitChance(),
     [ITEM_ATTRIBUTE_SHOOTRANGE] = i:getShootRange(),
     ["weight"] = i:getWeight(),
     ["attack"] = i:getAttack(),
     ["defense"] = i:getDefense(),
     ["extradefense"] = i:getExtraDefense(),
     ["armor"] = i:getArmor(),
     ["hitchance"] = i:getHitChance(),
     ["shootrange"] = i:getShootRange()
   }
 
   local attr = Item(uid):getAttribute(key)
   if tonumber(attr) then
     if numeric_attributes[key] then
       return attr ~= 0 and attr or numeric_attributes[key]
     end
   else
     if string_attributes[key] then
       if attr == "" then
         return string_attributes[key]
       end
     end
   end
return attr
end

function doItemSetAttribute(uid, key, value)
   return Item(uid):setAttribute(key, value)
end

function doItemEraseAttribute(uid, key)
   return Item(uid):removeAttribute(key)
end

2) Open data/global.lua and at the very top add this:
Code:
dofile('data/jewel_upgrading.lua')

3) Register the itemid for upgrading on actions.xml (You already have this done, I write it just in case)
Code:
<action itemid="XXXX" script="ZZZZ.lua"/> --XXXX for the itemid, ZZZZ for the script path

4) Inside the script for upgrading in data/actions/scripts, paste the original code, this one:
Code:
local conf = {
   ["level"] = {
   -- [item_level] = {successPercent= CHANCE TO UPGRADE ITEM, downgradeLevel = ITEM GETS THIS LEVEL IF UPGRADE FAILS}
     [1] = {successPercent = 85, downgradeLevel = 0},
     [2] = {successPercent = 80, downgradeLevel = 1},
     [3] = {successPercent = 75, downgradeLevel = 2},
     [4] = {successPercent = 70, downgradeLevel = 3},
     [5] = {successPercent = 65, downgradeLevel = 4},
     [6] = {successPercent = 60, downgradeLevel = 5},
     [7] = {successPercent = 55, downgradeLevel = 0},
     [8] = {successPercent = 50, downgradeLevel = 0},
     [9] = {successPercent = 45, downgradeLevel = 0}
   },

   ["upgrade"] = { -- how many percent attributes are rised?
     attack = 5, -- attack %
     defense = 5, -- defense %
     extraDefense = 10, -- extra defense %
     armor = 5, -- armor %
     hitChance = 5, -- hit chance %
   }
}



-- // do not touch // --
-- Upgrading system by Azi [Ersiu] --
-- Edited for TFS 1.1 by Zbizu --

local upgrading = {
  upValue = function (value, level, percent)
  if value < 0 then return 0 end
     if level == 0 then return value end
     local nVal = value
     for i = 1, level do
       nVal = nVal + (math.ceil((nVal/100*percent)))
     end
  return nVal > 0 and nVal or value
  end,

  getLevel = function (item)
  local name = Item(item):getName():split('+')
     if (#name == 1) then
       return 0
     end

     return math.abs(name[2])
  end,
}
function onUse(cid, item, fromPosition, itemEx, toPosition)
   local it = ItemType(itemEx.itemid)
  if((it:getWeaponType() > 0 or getItemAttribute(itemEx.uid, ITEM_ATTRIBUTE_ARMOR) > 0) and not isItemStackable(itemEx.itemid))then
  local level = upgrading.getLevel(itemEx.uid)
  if(level < #conf["level"])then
  local nLevel = (conf["level"][(level+1)].successPercent >= math.random(1,100)) and (level+1) or conf["level"][level].downgradeLevel
  if(nLevel > level)then
  doSendMagicEffect(toPosition, CONST_ME_MAGIC_GREEN)
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Upgrade to level " .. nLevel .. " successful!")
  else
  doSendMagicEffect(toPosition, CONST_ME_BLOCKHIT)
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Upgrade failed. Your " .. it:getName() .. " is now on level " .. nLevel .. "")
  end
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_NAME, it:getName()..((nLevel>0) and "+"..nLevel or ""))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_ATTACK,  upgrading.upValue(it:getAttack(), nLevel, conf["upgrade"].attack))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_DEFENSE, upgrading.upValue(it:getDefense(), nLevel, conf["upgrade"].defense))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_EXTRADEFENSE, upgrading.upValue(it:getExtraDefense(), nLevel, conf["upgrade"].extraDefense))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_ARMOR, upgrading.upValue(it:getArmor(), nLevel, conf["upgrade"].armor))
  doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_HITCHANCE, upgrading.upValue(it:getHitChance(), nLevel, conf["upgrade"].hitChance))
  doRemoveItem(item.uid, 1)
  else
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Your " .. it:getName() .. " is on max level alredy.")
  end
  else
  doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You cannot upgrade this item.")
  end
end

5) Remove the functions I made you add to compat.lua

6) Enjoy.
Thank you very much. Sorry for wasting you time :)
 
Back
Top