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

Lua Dont remove spaces script upgradesystem

maikons

Member
Joined
Aug 1, 2015
Messages
227
Reaction score
16
I have a problem on my upgrade system in VIP STONE (id: 8300)

When i fail with VIP stone(id 8300), it back to 0/0 (no atk/def upgraded) but
this script dont remove the spaces, add for script

Then if u fail 20x sword name stay it:
Code:
21:27 You see a thaian sword  +6 (Atk:52, Def:35).
It weighs 61.00 oz.
ItemID: [7391].
Position: [X: 982] [Y: 1013] [Z: 7].


SCRIPTS

Problem is here i think
part data/lib/upgradesystem.lua
Code:
  if item.itemid == 8300 then
  if self.item.level > 0 then
  self:setItemName(self:getItemName():gsub("%+(%d+)", ""))
  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.")

data/lib/upgradesystem.lua
FULL \/
Code:
PERFECT UPGRADE SYSTEM
  2.0
  Criado por Oneshot
  É proibido a venda ou a cópia sem os devidos créditos desse script.
]]--
UpgradeHandler = {
  levels = {
  [1] = {80, false, false},
  [2] = {75, false, false},
  [3] = {70, false, false},
  [4] = {60, true, false},
  [5] = {50, true, false},
  [6] = {35, true, false},
  [7] = {30, true, false},
  [8] = {25, true, true},
  [9] = {20, true, true},
  [10] = {15, true, true}
  },
  broadcast = 8,
  attributes = {
  ["attack"] = 1,
  ["defense"] = 1,
  ["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 = 0, removeable = true}}
  },
  isEquipment = function(self)
  local weaponType = self:getItemWeaponType()
  return ((weaponType > 0 and weaponType < 7) 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()
  self.item.level = (tonumber(self:getItemName():match("%+(%d+)")) or 0)
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(self.item.level < tool.range[1] or 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
  self:setItemName((self.item.level > 0 and self:getItemName():gsub("%+(%d+)", "+".. (self.item.level + 1)) or (self:getItemName() .." +1")))
  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
  self:setItemName(self:getItemName():gsub("%+(%d+)", ""))
  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

actions.xml
Code:
<action itemid="8306" event="script" value="upgrade.lua"/>
<action itemid="8300" event="script" value="upgrade.lua"/>

data/actions/scripts/upgrade.lua
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
   if isCreature(itemEx.uid) then  
     return doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)
   end

   local obj = UpgradeHandler:new(itemEx)

   if(obj == false) then
     return doPlayerSendCancel(cid, UpgradeHandler.message.notupgradeable)
   end

   local status = obj:refine(cid, item)
   if status == "success" then
     --doSendAnimatedText(toPosition, "Success!", COLOR_GREEN)
     doSendMagicEffect(toPosition, CONST_ME_MAGIC_GREEN)
   elseif status == "fail" then
     --doSendAnimatedText(toPosition, "Fail!", COLOR_RED)
     doSendMagicEffect(toPosition, CONST_ME_POFF)
   else
     doSendMagicEffect(toPosition, CONST_ME_POFF)
   end
   return true
end
 
Last edited:
Script is quite involved isn't it.

I think its this:
Code:
if self.item.level > 0 then
  self:setItemName(self:getItemName():gsub("%+(%d+)", ""))

Change it to this:
Code:
if self.item.level > 0 then
  self:setItemName(self:getItemName():gsub("(%s+)%+(%d+)", ""))

Now what does the item name look like?

_______

Here is a resource you can use to help tech yourself, or anyone else that wants to learn:
http://www.lua.org/cgi-bin/demo

Original script:
Code:
str = "thaian sword  +6"
str2 = str:gsub("%+(%d+)", "")
print(str)
print(str2)
Note the spaces still present in the output below.

New script:
Code:
str = "thaian sword  +6"
str2 = str:gsub("(%s+)%+(%d+)", "")
print(str)
print(str2)
Spaces are now gone.

In this case the issue was with the gsub arguments (finds pattern in a string and replaces it).
http://www.gammon.com.au/scripts/doc.php?lua=string.gsub
http://www.lua.org/pil/20.2.html

It was finding + and an integer and removing them ""

gsub("%+(%d+)", "")​

Nothing was telling it to find and replace spaces.

gsub("(%s+)%+(%d+)", "")​
 
Last edited:
Script is quite involved isn't it.

I think its this:
Code:
if self.item.level > 0 then
  self:setItemName(self:getItemName():gsub("%+(%d+)", ""))

Change it to this:
Code:
if self.item.level > 0 then
  self:setItemName(self:getItemName():gsub("(%s+)%+(%d+)", ""))

Now what does the item name look like?

_______

Here is a resource you can use to help tech yourself, or anyone else that wants to learn:
http://www.lua.org/cgi-bin/demo

Original script:
Code:
str = "thaian sword  +6"
str2 = str:gsub("%+(%d+)", "")
print(str)
print(str2)
Note the spaces still present in the output below.

New script:
Code:
str = "thaian sword  +6"
str2 = str:gsub("(%s+)%+(%d+)", "")
print(str)
print(str2)
Spaces are now gone.

In this case the issue was with the gsub arguments (finds pattern in a string and replaces it).
http://www.gammon.com.au/scripts/doc.php?lua=string.gsub
http://www.lua.org/pil/20.2.html

It was finding + and an integer and removing them ""

gsub("%+(%d+)", "")​

Nothing was telling it to find and replace spaces.

gsub("(%s+)%+(%d+)", "")​


Im so sorry to lost this topic man, so ty to help me

Now the script remove spaces, but dont downgrade upgrade, for exemple, my mace after use so stones (8300):

Code:
09:16 You see a mace (Atk:21, Def:16).
It weighs 28.00 oz.
ItemID: [2398].
Position: [X: 957] [Y: 1008] [Z: 7].

Normal mace:
Code:
09:17 You see a mace (Atk:16, Def:11).
It weighs 28.00 oz.
ItemID: [2398].
Position: [X: 957] [Y: 1008] [Z: 7].
 
Script is quite involved isn't it.

I think its this:
Code:
if self.item.level > 0 then
  self:setItemName(self:getItemName():gsub("%+(%d+)", ""))

Change it to this:
Code:
if self.item.level > 0 then
  self:setItemName(self:getItemName():gsub("(%s+)%+(%d+)", ""))

Now what does the item name look like?

_______

Here is a resource you can use to help tech yourself, or anyone else that wants to learn:
http://www.lua.org/cgi-bin/demo

Original script:
Code:
str = "thaian sword  +6"
str2 = str:gsub("%+(%d+)", "")
print(str)
print(str2)
Note the spaces still present in the output below.

New script:
Code:
str = "thaian sword  +6"
str2 = str:gsub("(%s+)%+(%d+)", "")
print(str)
print(str2)
Spaces are now gone.

In this case the issue was with the gsub arguments (finds pattern in a string and replaces it).
http://www.gammon.com.au/scripts/doc.php?lua=string.gsub
http://www.lua.org/pil/20.2.html

It was finding + and an integer and removing them ""

gsub("%+(%d+)", "")​

Nothing was telling it to find and replace spaces.

gsub("(%s+)%+(%d+)", "")​

I try too and have the same problem
 
I just made the changes suggested by @Leo32 with gsub and cleaned up the structure, while adding comments where needed. :)
Code:
    --PERFECT UPGRADE SYSTEM

    UpgradeHandler = {
        levels = {
              [1] = {80, false, false},
              [2] = {75, false, false},
              [3] = {70, false, false},
              [4] = {60, true, false},
              [5] = {50, true, false},
              [6] = {35, true, false},
              [7] = {30, true, false},
              [8] = {25, true, true},
              [9] = {20, true, true},
              [10] = {15, true, true}
        },
        broadcast = 8,
        attributes = {
              ["attack"] = 1,
              ["defense"] = 1,
              ["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 = 0, removeable = true}}
        },
        isEquipment = function(self)
            local weaponType = self:getItemWeaponType()
            return ((weaponType > 0 and weaponType < 7) 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 any number that has a + sign in front of it or 0 if there is no number with a + sign
        self.item.level = (tonumber(self:getItemName():match("%+(%d+)")) or 0)
    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(self.item.level < tool.range[1] or 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("%+(%d+)", "+".. (self.item.level + 1)) or (self:getItemName() .." +1")))
            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("(%s+)%+(%d+)", ""))
                    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
 
Last edited:
I just made the changes suggested by @Leo32 with gsub and cleaned up the structure, while adding comments where needed. :)
Code:
    --PERFECT UPGRADE SYSTEM

    UpgradeHandler = {
        levels = {
              [1] = {80, false, false},
              [2] = {75, false, false},
              [3] = {70, false, false},
              [4] = {60, true, false},
              [5] = {50, true, false},
              [6] = {35, true, false},
              [7] = {30, true, false},
              [8] = {25, true, true},
              [9] = {20, true, true},
              [10] = {15, true, true}
        },
        broadcast = 8,
        attributes = {
              ["attack"] = 1,
              ["defense"] = 1,
              ["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 = 0, removeable = true}}
        },
        isEquipment = function(self)
            local weaponType = self:getItemWeaponType()
            return ((weaponType > 0 and weaponType < 7) 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 any number that has a + sign in front of it or 0 if there is no number with a + sign
        self.item.level = (tonumber(self:getItemName():match("%+(%d+)")) or 0)
    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(self.item.level < tool.range[1] or 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("%+(%d+)", "+".. (self.item.level + 1)) or (self:getItemName() .." +1")))
            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("(%s+)%+(%d+)", ""))
                    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


TYYYYYYYYYYYY
 
Back
Top