• 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 0.X Problem on upgrade system

zabuzo

Well-Known Member
Joined
Jun 10, 2016
Messages
238
Reaction score
54
Using the stone 8300 when upgrade fail should back -1 lvl and remove the upgrade name...
Only remove -1 if there is a upgrade, for example golden armor (def 14) if i fail 2x it back to 12
I got one sucess and one fail, it should be normal
21:54 You see a golden armor (Arm:14).

But its showing:
21:54 You see a rare rare golden armor (Arm:14).

But it's not removing the name and it always removing the def

Anybody know what is wrong? This script is so hard to me..

Code:
--PERFECT UPGRADE SYSTEM
UpgradeHandler = {
    nameLv = {
        [1] = "rare",
        [2] = "perfect",
        [3] = "godlike"
    },
    levels = {
          [1] = {50, false, false},
          [2] = {20, false, false},
          [3] = {10, true, true}
    },
    broadcast = 4,
    attributes = {
          ["attack"] = 3,
          ["defense"] = 2,
          ["armor"] = 1
    },
    message = {
          console = "Trying to refine %s to level +%s with %s%% success rate.",
          success = "You have upgraded %s to level +%s",
          fail = "You have failed in upgrade of %s to level +%s",
          downgrade = "The upgrade level of %s has downgraded to +%s",
          erase = "The upgrade level of %s has been erased.",
          maxlevel = "The targeted %s is already on max upgrade level.",
          notupgradeable = "This item is not upgradeable.",
          broadcast = "The player %s was successful in upgrading %s to level +%s.\nCongratulations!!",
          invalidtool = "This is not a valid upgrade tool.",
          toolrange = "This upgrade tool can only be used in items with level between +%s and +%s"
    },
    tools = {
        [8306] = {range = {0, 10}, info = {chance = 0, removeable = true}},
        [8300] = {range = {0, 10}, info = {chance = 20, removeable = true}}
    },
    isEquipment = function(self)
        local weaponType = self:getItemWeaponType()
        return ((weaponType > 0 and weaponType < 8) or self.item.armor ~= 0)
    end,
    setItemName = function(self, name)
        return doItemSetAttribute(self.item.uid, "name", name)
    end,
    chance = function(self)
        local chances = {}
        chances.upgrade = (self.levels[self.item.level + 1][1] or 100)
        chances.downgrade = (self.item.level * 5)
        chances.erase = (self.item.level * 3)
        return chances
    end
}
function UpgradeHandler:new(item)
    local obj, ret = {}
    obj.item = {}
    obj.item.level = 0
    obj.item.uid = item.uid
    for key, value in pairs(getItemInfo(item.itemid)) do
        obj.item[key] = value
    end
    ret = setmetatable(obj, {
        __index = function(self, index)
            if _G[index] then
                return (setmetatable({callback = _G[index]},
                    {__call = function(self, ...)
                        return self.callback(item.uid, ...)
                    end}
                ))
            else
                return UpgradeHandler[index]
            end
    end})
    if ret:isEquipment() then
        ret:update()
        return ret
    end
    return false
end
function UpgradeHandler:update()
   -- this will return the level by the quality or 0 if it has no quality.
    self.item.level = 0
    for r, v in ipairs(self.nameLv) do
        if self:getItemName():find(v) then
            self.item.level = r
        end
    end
end
function UpgradeHandler:refine(uid, item)
    if not self.item then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.notupgradeable)
        return "miss"
    end
    local tool = self.tools[item.itemid]
    if(tool == nil) then
        doPlayerSendTextMessage(uid, MESSAGE_EVENT_DEFAULT, self.message.invalidtool)
        return "miss"
    end
    if(self.item.level > #self.levels) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.maxlevel:format(self.item.name))
        return "miss"
    end
    if(tool.range[1] and self.item.level < tool.range[1]) or (tool.range[2] and self.item.level >= tool.range[2]) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.toolrange:format(unpack(tool.range)))
        return "miss"
    end
    local chance = (self:chance().upgrade + tool.info.chance)
    doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.console:format(self.item.name, (self.item.level + 1), math.min(100, chance)))
    if(tool.info.removeable == true) then
        doRemoveItem(item.uid, 1)
    end
    if chance * 100 > math.random(1, 10000) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_ORANGE, self.message.success:format(self.item.name, (self.item.level + 1)))
        if (self.item.level + 1) >= self.broadcast then
            doBroadcastMessage(self.message.broadcast:format(getCreatureName(uid), self.item.name, (self.item.level + 1)))
        end
        -- it says if the item's level is greater then 0 (meaning is level equal to 1 or more) if it is add 1 more
        -- if the current level equals 0 add 1 to it
        --self:setItemName(self.item.level > 0 and (self:getItemName():gsub(self.nameLv[self.item.level], ""))..self.nameLv[self.item.level + 1] or self:getItemName().." "..self.nameLv[1])
        self:setItemName(self.item.level > 0 and self.nameLv[self.item.level + 1].." "..self:getItemName() or self.nameLv[1].." "..self:getItemName())
        for key, value in pairs(self.attributes) do
            if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                doItemSetAttribute(self.item.uid, key, (self.item.level > 0 and getItemAttribute(self.item.uid, key) or self.item[key]) + value)
            end
        end
        return "success"
    else
        if item.itemid == 8300 then
            if self.item.level > 0 then
                -- this will remove any number with a + sign in front of it from the string
                --self:setItemName(self:getItemName():gsub((" "..self.nameLv[self.item.level]), ""))
                self:setItemName(self.nameLv[self.item.level].." "..self:getItemName())
                for key, value in pairs(self.attributes) do
                    if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                        doItemSetAttribute(self.item.uid, key, getItemAttribute(self.item.uid, key) - self.item.level * value)
                    end
                end
            end
        else
            doRemoveItem(self.item.uid, 1)
        end
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, item.itemid == 8300 and "Your item level has been reseted." or "You have broken your item while trying to upgrade it.")
    end
end
 
Solution
Line 116
Lua:
self:setItemName(self.item.level > 0 and (self:self.nameLv[self.item.level + 1]:gsub(self.nameLv[self.item.level], ""))..getItemName() or self:self.nameLv[1].." "..getItemName())
Just a guess but shouldn't it be
Lua:
self:setItemName(self.item.level > 0 and (self.nameLv[self.item.level + 1]:gsub(self.nameLv[self.item.level], ""))..getItemName() or self.nameLv[1].." "..getItemName())
The colon [ : ] is normally used with methods, and the period [ . ] is normally used with properties.
This sure is more advanced than im used to, but i think the "self:" is pointing towards the specified item that we want to modify (like item.uid). Then again im learning by trial and error at this point, so dont quote me...
I was afraid this would happen when i did Edit the script, since i did remove som gsub function that would be the part where it sort the added names and replace/remove it. I’ll take a look when i got time

edit:
I did some changes to the script test whit a new item to see if it work correctly. it might fix existing broken item names aswell but i doubt it will. So i recommend you delete the bugged ones and manually replace it if a player got an upgraded item

Lua:
--PERFECT UPGRADE SYSTEM
UpgradeHandler = {
    nameLv = {
        [1] = "rare",
        [2] = "perfect",
        [3] = "godlike"
    },
    levels = {
          [1] = {50, false, false},
          [2] = {20, false, false},
          [3] = {10, true, true}
    },
    broadcast = 4,
    attributes = {
          ["attack"] = 3,
          ["defense"] = 2,
          ["armor"] = 1
    },
    message = {
          console = "Trying to refine %s to level +%s with %s%% success rate.",
          success = "You have upgraded %s to level +%s",
          fail = "You have failed in upgrade of %s to level +%s",
          downgrade = "The upgrade level of %s has downgraded to +%s",
          erase = "The upgrade level of %s has been erased.",
          maxlevel = "The targeted %s is already on max upgrade level.",
          notupgradeable = "This item is not upgradeable.",
          broadcast = "The player %s was successful in upgrading %s to level +%s.\nCongratulations!!",
          invalidtool = "This is not a valid upgrade tool.",
          toolrange = "This upgrade tool can only be used in items with level between +%s and +%s"
    },
    tools = {
        [8306] = {range = {0, 10}, info = {chance = 0, removeable = true}},
        [8300] = {range = {0, 10}, info = {chance = 20, removeable = true}}
    },
    isEquipment = function(self)
        local weaponType = self:getItemWeaponType()
        return ((weaponType > 0 and weaponType < 8) or self.item.armor ~= 0)
    end,
    setItemName = function(self, name)
        return doItemSetAttribute(self.item.uid, "name", name)
    end,
    chance = function(self)
        local chances = {}
        chances.upgrade = (self.levels[self.item.level + 1][1] or 100)
        chances.downgrade = (self.item.level * 5)
        chances.erase = (self.item.level * 3)
        return chances
    end
}
function UpgradeHandler:new(item)
    local obj, ret = {}
    obj.item = {}
    obj.item.level = 0
    obj.item.uid = item.uid
    for key, value in pairs(getItemInfo(item.itemid)) do
        obj.item[key] = value
    end
    ret = setmetatable(obj, {
        __index = function(self, index)
            if _G[index] then
                return (setmetatable({callback = _G[index]},
                    {__call = function(self, ...)
                        return self.callback(item.uid, ...)
                    end}
                ))
            else
                return UpgradeHandler[index]
            end
    end})
    if ret:isEquipment() then
        ret:update()
        return ret
    end
    return false
end
function UpgradeHandler:update()
   -- this will return the level by the quality or 0 if it has no quality.
    self.item.level = 0
    for r, v in ipairs(self.nameLv) do
        if self:getItemName():find(v) then
            self.item.level = r
        end
    end
end
function UpgradeHandler:refine(uid, item)
    if not self.item then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.notupgradeable)
        return "miss"
    end
    local tool = self.tools[item.itemid]
    if(tool == nil) then
        doPlayerSendTextMessage(uid, MESSAGE_EVENT_DEFAULT, self.message.invalidtool)
        return "miss"
    end
    if(self.item.level > #self.levels) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.maxlevel:format(self.item.name))
        return "miss"
    end
    if(tool.range[1] and self.item.level < tool.range[1]) or (tool.range[2] and self.item.level >= tool.range[2]) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.toolrange:format(unpack(tool.range)))
        return "miss"
    end
    local chance = (self:chance().upgrade + tool.info.chance)
    doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.console:format(self.item.name, (self.item.level + 1), math.min(100, chance)))
    if(tool.info.removeable == true) then
        doRemoveItem(item.uid, 1)
    end
    if chance * 100 > math.random(1, 10000) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_ORANGE, self.message.success:format(self.item.name, (self.item.level + 1)))
        if (self.item.level + 1) >= self.broadcast then
            doBroadcastMessage(self.message.broadcast:format(getCreatureName(uid), self.item.name, (self.item.level + 1)))
        end
        -- it says if the item's level is greater then 0 (meaning is level equal to 1 or more) if it is add 1 more
        -- if the current level equals 0 add 1 to it
        --self:setItemName(self.item.level > 0 and (self:getItemName():gsub(self.nameLv[self.item.level], ""))..self.nameLv[self.item.level + 1] or self:getItemName().." "..self.nameLv[1])
        self:setItemName(self.item.level > 0 and self.nameLv[self.item.level + 1]..(self:getItemName():gsub(self.nameLv[self.item.level], "")) or self.nameLv[1].." "..self:getItemName())
        for key, value in pairs(self.attributes) do
            if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                doItemSetAttribute(self.item.uid, key, (self.item.level > 0 and getItemAttribute(self.item.uid, key) or self.item[key]) + value)
            end
        end
        return "success"
    else
        if item.itemid == 8300 then
            if self.item.level > 0 then
                -- this will remove any number with a + sign in front of it from the string
                --self:setItemName(self:getItemName():gsub((" "..self.nameLv[self.item.level]), ""))
                self:setItemName(self:getItemName():gsub((self.nameLv[self.item.level].." "), ""))
                for key, value in pairs(self.attributes) do
                    if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                        doItemSetAttribute(self.item.uid, key, getItemAttribute(self.item.uid, key) - self.item.level * value)
                    end
                end
            end
        else
            doRemoveItem(self.item.uid, 1)
        end
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, item.itemid == 8300 and "Your item level has been reseted." or "You have broken your item while trying to upgrade it.")
    end
end

Edit:
Updated code
 
Last edited:
I was afraid this would happen when i did Edit the script, since i did remove som gsub function that would be the part where it sort the added names and replace/remove it. I’ll take a look when i got time

edit:
I did some changes to the script test whit a new item to see if it work correctly. it might fix existing broken item names aswell but i doubt it will. So i recommend you delete the bugged ones and manually replace it if a player got an upgraded item

Lua:
--PERFECT UPGRADE SYSTEM
UpgradeHandler = {
    nameLv = {
        [1] = "rare",
        [2] = "perfect",
        [3] = "godlike"
    },
    levels = {
          [1] = {50, false, false},
          [2] = {20, false, false},
          [3] = {10, true, true}
    },
    broadcast = 4,
    attributes = {
          ["attack"] = 3,
          ["defense"] = 2,
          ["armor"] = 1
    },
    message = {
          console = "Trying to refine %s to level +%s with %s%% success rate.",
          success = "You have upgraded %s to level +%s",
          fail = "You have failed in upgrade of %s to level +%s",
          downgrade = "The upgrade level of %s has downgraded to +%s",
          erase = "The upgrade level of %s has been erased.",
          maxlevel = "The targeted %s is already on max upgrade level.",
          notupgradeable = "This item is not upgradeable.",
          broadcast = "The player %s was successful in upgrading %s to level +%s.\nCongratulations!!",
          invalidtool = "This is not a valid upgrade tool.",
          toolrange = "This upgrade tool can only be used in items with level between +%s and +%s"
    },
    tools = {
        [8306] = {range = {0, 10}, info = {chance = 0, removeable = true}},
        [8300] = {range = {0, 10}, info = {chance = 20, removeable = true}}
    },
    isEquipment = function(self)
        local weaponType = self:getItemWeaponType()
        return ((weaponType > 0 and weaponType < 8) or self.item.armor ~= 0)
    end,
    setItemName = function(self, name)
        return doItemSetAttribute(self.item.uid, "name", name)
    end,
    chance = function(self)
        local chances = {}
        chances.upgrade = (self.levels[self.item.level + 1][1] or 100)
        chances.downgrade = (self.item.level * 5)
        chances.erase = (self.item.level * 3)
        return chances
    end
}
function UpgradeHandler:new(item)
    local obj, ret = {}
    obj.item = {}
    obj.item.level = 0
    obj.item.uid = item.uid
    for key, value in pairs(getItemInfo(item.itemid)) do
        obj.item[key] = value
    end
    ret = setmetatable(obj, {
        __index = function(self, index)
            if _G[index] then
                return (setmetatable({callback = _G[index]},
                    {__call = function(self, ...)
                        return self.callback(item.uid, ...)
                    end}
                ))
            else
                return UpgradeHandler[index]
            end
    end})
    if ret:isEquipment() then
        ret:update()
        return ret
    end
    return false
end
function UpgradeHandler:update()
   -- this will return the level by the quality or 0 if it has no quality.
    self.item.level = 0
    for r, v in ipairs(self.nameLv) do
        if self:getItemName():find(v) then
            self.item.level = r
        end
    end
end
function UpgradeHandler:refine(uid, item)
    if not self.item then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.notupgradeable)
        return "miss"
    end
    local tool = self.tools[item.itemid]
    if(tool == nil) then
        doPlayerSendTextMessage(uid, MESSAGE_EVENT_DEFAULT, self.message.invalidtool)
        return "miss"
    end
    if(self.item.level > #self.levels) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.maxlevel:format(self.item.name))
        return "miss"
    end
    if(tool.range[1] and self.item.level < tool.range[1]) or (tool.range[2] and self.item.level >= tool.range[2]) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.toolrange:format(unpack(tool.range)))
        return "miss"
    end
    local chance = (self:chance().upgrade + tool.info.chance)
    doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.console:format(self.item.name, (self.item.level + 1), math.min(100, chance)))
    if(tool.info.removeable == true) then
        doRemoveItem(item.uid, 1)
    end
    if chance * 100 > math.random(1, 10000) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_ORANGE, self.message.success:format(self.item.name, (self.item.level + 1)))
        if (self.item.level + 1) >= self.broadcast then
            doBroadcastMessage(self.message.broadcast:format(getCreatureName(uid), self.item.name, (self.item.level + 1)))
        end
        -- it says if the item's level is greater then 0 (meaning is level equal to 1 or more) if it is add 1 more
        -- if the current level equals 0 add 1 to it
        --self:setItemName(self.item.level > 0 and (self:getItemName():gsub(self.nameLv[self.item.level], ""))..self.nameLv[self.item.level + 1] or self:getItemName().." "..self.nameLv[1])
        self:setItemName(self.item.level > 0 and (self:getItemName():gsub(self.nameLv[self.item.level], self.nameLv[self.item.level + 1])) or self:self.nameLv[1].." "..getItemName())
        for key, value in pairs(self.attributes) do
            if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                doItemSetAttribute(self.item.uid, key, (self.item.level > 0 and getItemAttribute(self.item.uid, key) or self.item[key]) + value)
            end
        end
        return "success"
    else
        if item.itemid == 8300 then
            if self.item.level > 0 then
                -- this will remove any number with a + sign in front of it from the string
                --self:setItemName(self:getItemName():gsub((" "..self.nameLv[self.item.level]), ""))
                self:setItemName(self:getItemName():gsub((self.nameLv[self.item.level].." "), ""))
                for key, value in pairs(self.attributes) do
                    if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                        doItemSetAttribute(self.item.uid, key, getItemAttribute(self.item.uid, key) - self.item.level * value)
                    end
                end
            end
        else
            doRemoveItem(self.item.uid, 1)
        end
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, item.itemid == 8300 and "Your item level has been reseted." or "You have broken your item while trying to upgrade it.")
    end
end

Thank you to help me again!
But it's showing a error:
Code:
[15:28:53.119] >> Opening logs
[Error - LuaInterface::loadFile] data/lib/upgradesystem.lua:116: function arguments expected near '.'
 
Thank you to help me again!
But it's showing a error:
Code:
[15:28:53.119] >> Opening logs
[Error - LuaInterface::loadFile] data/lib/upgradesystem.lua:116: function arguments expected near '.'
Test this one, not sure if it still will bug though since im not to familiar whit this gsub function
Lua:
--PERFECT UPGRADE SYSTEM
UpgradeHandler = {
    nameLv = {
        [1] = "rare",
        [2] = "perfect",
        [3] = "godlike"
    },
    levels = {
          [1] = {50, false, false},
          [2] = {20, false, false},
          [3] = {10, true, true}
    },
    broadcast = 4,
    attributes = {
          ["attack"] = 3,
          ["defense"] = 2,
          ["armor"] = 1
    },
    message = {
          console = "Trying to refine %s to level +%s with %s%% success rate.",
          success = "You have upgraded %s to level +%s",
          fail = "You have failed in upgrade of %s to level +%s",
          downgrade = "The upgrade level of %s has downgraded to +%s",
          erase = "The upgrade level of %s has been erased.",
          maxlevel = "The targeted %s is already on max upgrade level.",
          notupgradeable = "This item is not upgradeable.",
          broadcast = "The player %s was successful in upgrading %s to level +%s.\nCongratulations!!",
          invalidtool = "This is not a valid upgrade tool.",
          toolrange = "This upgrade tool can only be used in items with level between +%s and +%s"
    },
    tools = {
        [8306] = {range = {0, 10}, info = {chance = 0, removeable = true}},
        [8300] = {range = {0, 10}, info = {chance = 20, removeable = true}}
    },
    isEquipment = function(self)
        local weaponType = self:getItemWeaponType()
        return ((weaponType > 0 and weaponType < 8) or self.item.armor ~= 0)
    end,
    setItemName = function(self, name)
        return doItemSetAttribute(self.item.uid, "name", name)
    end,
    chance = function(self)
        local chances = {}
        chances.upgrade = (self.levels[self.item.level + 1][1] or 100)
        chances.downgrade = (self.item.level * 5)
        chances.erase = (self.item.level * 3)
        return chances
    end
}
function UpgradeHandler:new(item)
    local obj, ret = {}
    obj.item = {}
    obj.item.level = 0
    obj.item.uid = item.uid
    for key, value in pairs(getItemInfo(item.itemid)) do
        obj.item[key] = value
    end
    ret = setmetatable(obj, {
        __index = function(self, index)
            if _G[index] then
                return (setmetatable({callback = _G[index]},
                    {__call = function(self, ...)
                        return self.callback(item.uid, ...)
                    end}
                ))
            else
                return UpgradeHandler[index]
            end
    end})
    if ret:isEquipment() then
        ret:update()
        return ret
    end
    return false
end
function UpgradeHandler:update()
   -- this will return the level by the quality or 0 if it has no quality.
    self.item.level = 0
    for r, v in ipairs(self.nameLv) do
        if self:getItemName():find(v) then
            self.item.level = r
        end
    end
end
function UpgradeHandler:refine(uid, item)
    if not self.item then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.notupgradeable)
        return "miss"
    end
    local tool = self.tools[item.itemid]
    if(tool == nil) then
        doPlayerSendTextMessage(uid, MESSAGE_EVENT_DEFAULT, self.message.invalidtool)
        return "miss"
    end
    if(self.item.level > #self.levels) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.maxlevel:format(self.item.name))
        return "miss"
    end
    if(tool.range[1] and self.item.level < tool.range[1]) or (tool.range[2] and self.item.level >= tool.range[2]) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.toolrange:format(unpack(tool.range)))
        return "miss"
    end
    local chance = (self:chance().upgrade + tool.info.chance)
    doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.console:format(self.item.name, (self.item.level + 1), math.min(100, chance)))
    if(tool.info.removeable == true) then
        doRemoveItem(item.uid, 1)
    end
    if chance * 100 > math.random(1, 10000) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_ORANGE, self.message.success:format(self.item.name, (self.item.level + 1)))
        if (self.item.level + 1) >= self.broadcast then
            doBroadcastMessage(self.message.broadcast:format(getCreatureName(uid), self.item.name, (self.item.level + 1)))
        end
        -- it says if the item's level is greater then 0 (meaning is level equal to 1 or more) if it is add 1 more
        -- if the current level equals 0 add 1 to it
        --self:setItemName(self.item.level > 0 and (self:getItemName():gsub(self.nameLv[self.item.level], ""))..self.nameLv[self.item.level + 1] or self:getItemName().." "..self.nameLv[1])
        self:setItemName(self.item.level > 0 and self.nameLv[self.item.level + 1]..(self:getItemName():gsub(self.nameLv[self.item.level], "")) or self.nameLv[1].." "..self:getItemName())
        for key, value in pairs(self.attributes) do
            if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                doItemSetAttribute(self.item.uid, key, (self.item.level > 0 and getItemAttribute(self.item.uid, key) or self.item[key]) + value)
            end
        end
        return "success"
    else
        if item.itemid == 8300 then
            if self.item.level > 0 then
                -- this will remove any number with a + sign in front of it from the string
                --self:setItemName(self:getItemName():gsub((" "..self.nameLv[self.item.level]), ""))
                self:setItemName(self:getItemName():gsub((self.nameLv[self.item.level].." "), ""))
                for key, value in pairs(self.attributes) do
                    if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                        doItemSetAttribute(self.item.uid, key, getItemAttribute(self.item.uid, key) - self.item.level * value)
                    end
                end
            end
        else
            doRemoveItem(self.item.uid, 1)
        end
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, item.itemid == 8300 and "Your item level has been reseted." or "You have broken your item while trying to upgrade it.")
    end
end

Edit:
updated code both posts should contain same code now
 
Last edited:
Test this one, not sure if it still will bug though since im not to familiar whit this gsub function
Lua:
--PERFECT UPGRADE SYSTEM
UpgradeHandler = {
    nameLv = {
        [1] = "rare",
        [2] = "perfect",
        [3] = "godlike"
    },
    levels = {
          [1] = {50, false, false},
          [2] = {20, false, false},
          [3] = {10, true, true}
    },
    broadcast = 4,
    attributes = {
          ["attack"] = 3,
          ["defense"] = 2,
          ["armor"] = 1
    },
    message = {
          console = "Trying to refine %s to level +%s with %s%% success rate.",
          success = "You have upgraded %s to level +%s",
          fail = "You have failed in upgrade of %s to level +%s",
          downgrade = "The upgrade level of %s has downgraded to +%s",
          erase = "The upgrade level of %s has been erased.",
          maxlevel = "The targeted %s is already on max upgrade level.",
          notupgradeable = "This item is not upgradeable.",
          broadcast = "The player %s was successful in upgrading %s to level +%s.\nCongratulations!!",
          invalidtool = "This is not a valid upgrade tool.",
          toolrange = "This upgrade tool can only be used in items with level between +%s and +%s"
    },
    tools = {
        [8306] = {range = {0, 10}, info = {chance = 0, removeable = true}},
        [8300] = {range = {0, 10}, info = {chance = 20, removeable = true}}
    },
    isEquipment = function(self)
        local weaponType = self:getItemWeaponType()
        return ((weaponType > 0 and weaponType < 8) or self.item.armor ~= 0)
    end,
    setItemName = function(self, name)
        return doItemSetAttribute(self.item.uid, "name", name)
    end,
    chance = function(self)
        local chances = {}
        chances.upgrade = (self.levels[self.item.level + 1][1] or 100)
        chances.downgrade = (self.item.level * 5)
        chances.erase = (self.item.level * 3)
        return chances
    end
}
function UpgradeHandler:new(item)
    local obj, ret = {}
    obj.item = {}
    obj.item.level = 0
    obj.item.uid = item.uid
    for key, value in pairs(getItemInfo(item.itemid)) do
        obj.item[key] = value
    end
    ret = setmetatable(obj, {
        __index = function(self, index)
            if _G[index] then
                return (setmetatable({callback = _G[index]},
                    {__call = function(self, ...)
                        return self.callback(item.uid, ...)
                    end}
                ))
            else
                return UpgradeHandler[index]
            end
    end})
    if ret:isEquipment() then
        ret:update()
        return ret
    end
    return false
end
function UpgradeHandler:update()
   -- this will return the level by the quality or 0 if it has no quality.
    self.item.level = 0
    for r, v in ipairs(self.nameLv) do
        if self:getItemName():find(v) then
            self.item.level = r
        end
    end
end
function UpgradeHandler:refine(uid, item)
    if not self.item then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.notupgradeable)
        return "miss"
    end
    local tool = self.tools[item.itemid]
    if(tool == nil) then
        doPlayerSendTextMessage(uid, MESSAGE_EVENT_DEFAULT, self.message.invalidtool)
        return "miss"
    end
    if(self.item.level > #self.levels) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.maxlevel:format(self.item.name))
        return "miss"
    end
    if(tool.range[1] and self.item.level < tool.range[1]) or (tool.range[2] and self.item.level >= tool.range[2]) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.toolrange:format(unpack(tool.range)))
        return "miss"
    end
    local chance = (self:chance().upgrade + tool.info.chance)
    doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.console:format(self.item.name, (self.item.level + 1), math.min(100, chance)))
    if(tool.info.removeable == true) then
        doRemoveItem(item.uid, 1)
    end
    if chance * 100 > math.random(1, 10000) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_ORANGE, self.message.success:format(self.item.name, (self.item.level + 1)))
        if (self.item.level + 1) >= self.broadcast then
            doBroadcastMessage(self.message.broadcast:format(getCreatureName(uid), self.item.name, (self.item.level + 1)))
        end
        -- it says if the item's level is greater then 0 (meaning is level equal to 1 or more) if it is add 1 more
        -- if the current level equals 0 add 1 to it
        --self:setItemName(self.item.level > 0 and (self:getItemName():gsub(self.nameLv[self.item.level], ""))..self.nameLv[self.item.level + 1] or self:getItemName().." "..self.nameLv[1])
        self:setItemName(self.item.level > 0 and (self:self.nameLv[self.item.level + 1]:gsub(self.nameLv[self.item.level], ""))..getItemName() or self:self.nameLv[1].." "..getItemName())
        for key, value in pairs(self.attributes) do
            if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                doItemSetAttribute(self.item.uid, key, (self.item.level > 0 and getItemAttribute(self.item.uid, key) or self.item[key]) + value)
            end
        end
        return "success"
    else
        if item.itemid == 8300 then
            if self.item.level > 0 then
                -- this will remove any number with a + sign in front of it from the string
                --self:setItemName(self:getItemName():gsub((" "..self.nameLv[self.item.level]), ""))
                self:setItemName(self:getItemName():gsub((self.nameLv[self.item.level].." "), ""))
                for key, value in pairs(self.attributes) do
                    if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                        doItemSetAttribute(self.item.uid, key, getItemAttribute(self.item.uid, key) - self.item.level * value)
                    end
                end
            end
        else
            doRemoveItem(self.item.uid, 1)
        end
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, item.itemid == 8300 and "Your item level has been reseted." or "You have broken your item while trying to upgrade it.")
    end
end

Edit:
both posts should contain same code now

Still happen:
Code:
[15:51:03.847] >> Opening logs
[Error - LuaInterface::loadFile] data/lib/upgradesystem.lua:116: function arguments expected near '.'
^C
 
Line 116
Lua:
self:setItemName(self.item.level > 0 and (self:self.nameLv[self.item.level + 1]:gsub(self.nameLv[self.item.level], ""))..getItemName() or self:self.nameLv[1].." "..getItemName())
Just a guess but shouldn't it be
Lua:
self:setItemName(self.item.level > 0 and (self.nameLv[self.item.level + 1]:gsub(self.nameLv[self.item.level], ""))..getItemName() or self.nameLv[1].." "..getItemName())
The colon [ : ] is normally used with methods, and the period [ . ] is normally used with properties.
 
Line 116
Lua:
self:setItemName(self.item.level > 0 and (self:self.nameLv[self.item.level + 1]:gsub(self.nameLv[self.item.level], ""))..getItemName() or self:self.nameLv[1].." "..getItemName())
Just a guess but shouldn't it be
Lua:
self:setItemName(self.item.level > 0 and (self.nameLv[self.item.level + 1]:gsub(self.nameLv[self.item.level], ""))..getItemName() or self.nameLv[1].." "..getItemName())
The colon [ : ] is normally used with methods, and the period [ . ] is normally used with properties.

Now no errors to open, but when i try to upgrade a item
17:19 You have upgraded plate armor to level +1

Plate armor still:
17:20 You see a plate armor (Arm:10).

On console is printing this error:
Code:
[17:21:30.376] [Error - Action Interface]
[17:21:30.376] data/actions/scripts/upgrade.lua:onUse
[17:21:30.376] Description:
[17:21:30.376] (luaGetThing) Thing not found

[17:21:30.376] [Error - Action Interface]
[17:21:30.377] data/actions/scripts/upgrade.lua:onUse
[17:21:30.377] Description:
[17:21:30.377] data/lib/050-function.lua:253: attempt to index a boolean value
[17:21:30.377] stack traceback:
[17:21:30.377]    data/lib/050-function.lua:253: in function 'getItemName'
[17:21:30.377]    data/lib/upgradesystem.lua:116: in function 'refine'
[17:21:30.377]    data/actions/scripts/upgrade.lua:11: in function <data/actions/scripts/upgrade.lua:1>
 
Line 116
Lua:
self:setItemName(self.item.level > 0 and (self:self.nameLv[self.item.level + 1]:gsub(self.nameLv[self.item.level], ""))..getItemName() or self:self.nameLv[1].." "..getItemName())
Just a guess but shouldn't it be
Lua:
self:setItemName(self.item.level > 0 and (self.nameLv[self.item.level + 1]:gsub(self.nameLv[self.item.level], ""))..getItemName() or self.nameLv[1].." "..getItemName())
The colon [ : ] is normally used with methods, and the period [ . ] is normally used with properties.
This sure is more advanced than im used to, but i think the "self:" is pointing towards the specified item that we want to modify (like item.uid). Then again im learning by trial and error at this point, so dont quote me on that . :p
But im not even testing the system on my own, wich i should do to get a better understanding how it actually works. But since im not going to use this system im to lazy ;)

edit:
@zabuzo what happens if you change line 116 for this instead
Lua:
self:setItemName(self.item.level > 0 and self.nameLv[self.item.level + 1]..(self:getItemName():gsub(self.nameLv[self.item.level], "")) or self.nameLv[1].." "..self:getItemName())

Edit2:
check above posts for full script, any script posted in this thread by me are all edited to the same ( hopefully 100% bug free :p )
 
Last edited:
Solution
This sure is more advanced than im used to, but i think the "self:" is pointing towards the specified item that we want to modify (like item.uid). Then again im learning by trial and error at this point, so dont quote me on that . :p
But im not even testing the system on my own, wich i should do to get a better understanding how it actually works. But since im not going to use this system im to lazy ;)

edit:
@zabuzo what happens if you change line 116 for this instead
Lua:
self:setItemName(self.item.level > 0 and self.nameLv[self.item.level + 1]..(self:getItemName():gsub(self.nameLv[self.item.level], "")) or self.nameLv[1].." "..self:getItemName())

WORKING THANKS!
 
Back
Top