• 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!

TFS 1.2 getItemAttribute item upgrading

Verrine

Member
Joined
Mar 2, 2016
Messages
117
Reaction score
7
Hi!
I found here verry nice script for tfs 1.1 but it dont work on 1.2
Here is the script : https://otland.net/threads/tfs-1-1-items-upgrading-by-jewels.228712/

I've copied to global.lua everything and did item but when i try to use it i get this error in console:
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/upgrading.lua:onUse
data/global.lua:123: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/global.lua:123: in function 'getItemAttribute'
        data/actions/scripts/upgrading.lua:52: in function <data/actions/scripts/upgrading.lua:50>

i think there is a problem in global.lua in here:
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()
   }

but i rly dont know whats going wrong here. If here is someone who know whats happen please let me know how to fix it. Im new in lua and tfs so many maybe simple errors giving me a lot of troubles ;/ Thanks.

EDIT:
Okay i changed getitemattribute to Item(item.uid) and there is another error:
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/upgrading.lua:onUse
data/actions/scripts/upgrading.lua:52: attempt to call method 'getWeaponType' (a nil value)
stack traceback:
        [C]: in function 'getWeaponType'
        data/actions/scripts/upgrading.lua:52: in function <data/actions/scripts/upgrading.lua:50>
upgrading lua:
Code:
function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
   local it = Item(item.uid)
  if((it:getWeaponType(item.uid) > 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


just let me know how can i get Weapon Type in new tfs 1.2 couse from 1.0 to 1.1 it was GetWeaponType() and now... ?
 
Last edited:
In onUse encapsulate the entire condition block after local it = Item(item.uid) with
Code:
if it then
-- block of code
end
and see if you still get that error
 
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/upgrading.lua:onUse
data/actions/scripts/upgrading.lua:42: attempt to index a nil value
stack traceback:
  [C]: in function '__index'
  data/actions/scripts/upgrading.lua:42: in function 'getLevel'
  data/actions/scripts/upgrading.lua:54: in function <data/actions/scripts/upgrading.lua:50>
42 is local name = Item(item):getName():split('+') in :
Code:
  getLevel = function (item)
  local name = Item(item):getName():split('+')
  if (#name == 1) then
  return 0
  end
 
  return math.abs(name[2])
  end,
}
 
Last edited:
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/upgrading.lua:onUse
data/actions/scripts/upgrading.lua:42: attempt to index a nil value
stack traceback:
  [C]: in function '__index'
  data/actions/scripts/upgrading.lua:42: in function 'getLevel'
  data/actions/scripts/upgrading.lua:54: in function <data/actions/scripts/upgrading.lua:50>
42 is local name = Item(item):getName():split('+') in :
Code:
  getLevel = function (item)
  local name = Item(item):getName():split('+')
  if (#name == 1) then
  return 0
  end

  return math.abs(name[2])
  end,
}
The Item(item) is attempting to construct a new item, if the item does not exist then of course it will throw an error when you chain a metamethod to it.

To prevent this always check if it exist 1st then apply a chained metamethod if any.
Code:
getLevel = function (item)
    local it, name = Item(item), ''
    if it then -- encapsulate start
        name = it:getName():split('+')
        if (#name == 1) then
            return 0
        end
    end -- encapsulate end
Get into the habit of checking if the object exist before applying a chained metamethod.
 
[Warning - Event::checkScript] Can not load script: scripts/upgrading.lua
data/actions/scripts/upgrading.lua:50: unexpected symbol near '}'
seems like it doesnt want to be in local upgrading or duno

Code:
local conf = {
   ["level"] = {
   -- [item_level] = {successPercent= CHANCE TO UPGRADE ITEM, downgradeLevel = ITEM GETS THIS LEVEL IF UPGRADE FAILS}
     [1] = {successPercent = 80, downgradeLevel = 0},
     [2] = {successPercent = 70, downgradeLevel = 1},
     [3] = {successPercent = 70, downgradeLevel = 2},
     [4] = {successPercent = 65, 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 %
   }
}

--local cid as player:getId()

-- // 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 it, name = Item(item), ''
    if it then
        name = it:getName():split('+')
        if (#name == 1) then
            return 0
        end
    --end
}


function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
   local it = Item(item.uid)
  if it then
  --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

there is a whole file couse i guess it might be problem with file fragments
 
Last edited:
Did you forget to copy the required functions?
Code:
-- required functions
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

-----------------------------------

local c = {
    ["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 it, name = Item(item), ''
        if it then
            name = it:getName():split('+')
            if #name == 1 then
                return 0
            end
        else
            return 0
        end
        return math.abs(name[2])
    end
}

function onUse(player, item, fromPosition, itemEx, toPosition)
    local it = ItemType(itemEx.itemid)
    if it then
        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 < #c["level"])then
                local nLevel = (c["level"][(level+1)].successPercent >= math.random(1,100)) and (level+1) or c["level"][level].downgradeLevel
                if(nLevel > level)then
                    toPosition:sendMagicEffect(CONST_ME_MAGIC_GREEN)
                    player:sendTextMessage(MESSAGE_INFO_DESCR, "Upgrade to level " .. nLevel .. " successful!")
                else
                    toPosition:sendMagicEffect(CONST_ME_BLOCKHIT)
                    player:sendTextMessage(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, c["upgrade"].attack))
                doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_DEFENSE, upgrading.upValue(it:getDefense(), nLevel, c["upgrade"].defense))
                doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_EXTRADEFENSE, upgrading.upValue(it:getExtraDefense(), nLevel, c["upgrade"].extraDefense))
                doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_ARMOR, upgrading.upValue(it:getArmor(), nLevel, c["upgrade"].armor))
                doItemSetAttribute(itemEx.uid, ITEM_ATTRIBUTE_HITCHANCE, upgrading.upValue(it:getHitChance(), nLevel, c["upgrade"].hitChance))
                doRemoveItem(item.uid, 1)
            else
                player:sendTextMessage(MESSAGE_INFO_DESCR, "Your " .. it:getName() .. " is on max level alredy.")
            end
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot upgrade this item.")
        end
    end
    player:sendTextMessage(MESSAGE_INFO_DESCR, "Sorry not possible.")
    return true
end
 
Code:
        [C]: in function '__index'
        data/actions/scripts/upgrading.lua:3: in function 'getItemAttribute'
        data/actions/scripts/upgrading.lua:111: in function <data/actions/scripts/upgrading.lua:108>

this is when i pasted a whole file that you posted

and when it was only in global.lua same error that i posted in my last post
 
The Item(item) is attempting to construct a new item, if the item does not exist then of course it will throw an error when you chain a metamethod to it.

To prevent this always check if it exist 1st then apply a chained metamethod if any.
Code:
getLevel = function (item)
    local it, name = Item(item), ''
    if it then -- encapsulate start
        name = it:getName():split('+')
        if (#name == 1) then
            return 0
        end
    end -- encapsulate end
Get into the habit of checking if the object exist before applying a chained metamethod.
I feel like I am talking to myself.
 
I feel like I am talking to myself.
>> Loading script systems
[Warning - Event::checkScript] Can not load script: scripts/upgrading.lua
data/actions/scripts/upgrading.lua:102: unexpected symbol near '}'


sorry i had to go study thats why i did it fast and missed something.
When i changet everything as You told me here is an error

maybe i dont understand this code now ;/
 
maybe i dont understand this code now ;/
No you just don't understand the code at all, and I over looked a possible error in the needed functions before I added it to the script, either way it doesn't matter.

My work is done here.
 
No you just don't understand the code at all, and I over looked a possible error in the needed functions before I added it to the script, either way it doesn't matter.

My work is done here.
added it before You posted a whole new code im just posting all errors lol why are You so mad, not everyone are pro with programming ive never told that. Anyway Thanks for Your help.
 
added it before You posted a whole new code im just posting all errors lol why are You so mad, not everyone are pro with programming ive never told that. Anyway Thanks for Your help.
I am not a programming pro, nor am I mad, I just see no point trying to resolve an issue for you since I already stated how to resolve it prior to adding in the needed functions, and then when I realized the needed functions could possibly be erroneous i figured.. why am I going out of my way to explain how to fix x or y when you don't even care or just don't have the knowledge to understand what I am talking about anway.

TLDR
I have to get ready for work.
 
I am not a programming pro, nor am I mad, I just see no point trying to resolve an issue for you since I already stated how to resolve it prior to adding in the needed functions, and then when I realized the needed functions could possibly be erroneous i figured.. why am I going out of my way to explain how to fix x or y when you don't even care or just don't have the knowledge to understand what I am talking about anway.

TLDR
I have to get ready for work.
anyway sorry if we missunderstanded ourselves im not native english speaker also so learning in not my language is harder for me ;< Yes its the second one probably i dont know how to fix something becouse i ve never written any big code in lua? i know some pawn etc but not lua with many refferences(?) in other plugins ;<
 
Back
Top