• Sign up for TFS Icon Competition! The prize includes Otland Premium time and a medal, as well as obviously taking part in TFS history.
  • 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 cut/get a part of string lua (item name script)

samandriel

Active Member
Joined
Oct 19, 2016
Messages
223
Solutions
1
Reaction score
45
I have to check if in the start there is a [RARE], [EPIC] or [LEGENDARY]
How to?
I tried this with some part of scripts i found here:
Code:
function getRarity(str)
    print("starts getRarity")
    local value = 0
    local n = str:match("%[(.-)%]")
    print("rarity:")
    print(n)
    if n == "RARE" then
       value = 1
    elseif n == "EPIC" then
       value = 2
    elseif n == "LEGENDARY" then
       value = 3
    end
    print("value:")
    print(value)
    print("ends getRarity")
    return value
end

But when i try to to test this script with a [RARE] battle axe:
Code:
08:30 You see a [RARE] battle axe (Atk:28, Def:12). It weighs 50.00 oz.
08:31 You successfully add 1 battle axe for 500 gps to offerts database.

It prints:
Code:
2378
1
battle axe
battle axe
starts getRarity
rarity:

value:
0
ends getRarity
0

what is wrong?

full script:
Code:
--[[
Offline player to player item trader (Auction System) by vDk
Script version: 1.2a [ -- FIXED CLONE ITEMS BUG -- ]
]]--
local config = {
    levelRequiredToAdd = 5,
    SendOffersOnlyInPZ = true,
    blocked_items = {2165, 2152, 2148, 2160, 2166, 2167, 2168, 2169, 2202, 2203, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212, 2213, 2214, 2215, 2343, 2433, 2640, 6132, 6300, 6301, 9932, 9933, 6093, 2123}
}

local function updatePlayerBalance(name, value)
    db.query('UPDATE players SET balance=' .. value .. ' WHERE name=' .. db.escapeString(name) .. ' LIMIT 1')
end

function getRarity(str)
    print("starts getRarity")
    local value = 0
    local n = str:match("%[(.-)%]")
    print("rarity:")
    print(n)
    if n == "RARE" then
       value = 1
    elseif n == "EPIC" then
       value = 2
    elseif n == "LEGENDARY" then
       value = 3
    end
    print("value:")
    print(value)
    print("ends getRarity")
    return value
end
 
function onSay(cid, words, param, channel)
    if(param == '') then
        local msg = "Market:\n\n/market buy, ID\n/market remove, ID\n/market add, ItemPrice\n\nMore information look in us website!"
        doPlayerPopupFYI(cid, msg)
        return true
    end

    local maxOffersPerPlayer = math.floor(getPlayerLevel(cid) / 5)
    local pricePerOffer = 500
 
    local t = string.explode(param, ",")
    if(t[1] == "add") then
        local Item_Price = t[2]

        if(not Item_Price) then
            local msg = "/market add, ItemPrice"
            doPlayerPopupFYI(cid, msg)
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, msg)
            return true
        end
 
        if(not tonumber(Item_Price)) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't set valid price or items count.")
            return true
        end
 
        if(string.len(Item_Price) > 7) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This price or item count is too high.")
            return true
        end

        if getPlayerSlotItem(cid, CONST_SLOT_AMMO).itemid == 0 then
            return doPlayerSendCancel(cid, "You have no item in your equipament arrow slot to sell!")
        end
        
        local item = getPlayerSlotItem(cid, CONST_SLOT_AMMO).itemid
        print(item)

        local Item_Count = getPlayerSlotItem(cid, CONST_SLOT_AMMO).type
        if(tonumber(Item_Count) < 1) then
            Item_Count = 1
        end
        print(Item_Count)
        local Item_Name = getItemNameById(item)
        print(Item_Name)
 
        if(getPlayerLevel(cid) < config.levelRequiredToAdd) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't have required (" .. config.levelRequiredToAdd .. ") level.")
            return true
        end
 
        if(isInArray(config.blocked_items, item)) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This item is blocked.")
            return true
        end
 
        local check = db.getResult("SELECT `id` FROM `auction_system` WHERE `player` = " .. getPlayerGUID(cid) .. ";")
        if(check:getID() == -1) then
        elseif(check:getRows(true) >= maxOffersPerPlayer) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry you can't add more offers (max for your level: " .. maxOffersPerPlayer .. ")")
            return true
        end
 
        if(config.SendOffersOnlyInPZ) then
            if(not getTilePzInfo(getPlayerPosition(cid))) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must be in PZ area when you add offert to database.")
                return true
            end
        end
 
        if((tonumber(Item_Price) < 1)) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You have to type a number higher than 0.")
            return true
        end

        if(getPlayerBalance(cid) < 500) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You need to have 500GPs in your bank balance to make a offer.")
            return true
        end

        -- check if it is enchanted
        -- check if before name there is:
        -- [RARE] itemname
        -- [EPIC] itemname
        -- [LEGENDARY] itemname

        local itemname = Item_Name
        print(itemname)
        local test = getRarity(itemname)
        print(test)

        doPlayerSetBalance(cid, getPlayerBalance(cid) - 500)
        updatePlayerBalance(getCreatureByName(cid), getPlayerBalance(cid))
 
        local itemcount, costgp = math.floor(Item_Count), math.floor(Item_Price)
        doPlayerRemoveItem(cid, item, itemcount)
        db.executeQuery("INSERT INTO `auction_system` (`player`, `item_name`, `item_id`, `count`, `cost`, `date`) VALUES (" .. getPlayerGUID(cid) .. ", \"" .. Item_Name .. "\", " .. getItemIdByName(Item_Name) .. ", " .. itemcount .. ", " .. costgp ..", " .. os.time() .. ")")
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You successfully add " .. itemcount .." " .. Item_Name .." for " .. costgp .. " gps to offerts database.")
    end
 
    if(t[1] == "buy") then
        if(not tonumber(t[2])) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
            return true
        end
 
        local buy = db.getResult("SELECT * FROM `auction_system` WHERE `id` = " .. (tonumber(t[2])) .. ";")
        if(buy:getID() ~= -1) then
            if (getPlayerBalance(cid) < buy:getDataInt("cost")) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't have enoguh Bank Balance.")
                buy:free()
                return true
            end
 
            if(getPlayerName(cid) == getPlayerNameByGUID(buy:getDataInt("player"))) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, you can't buy your own items.")
                buy:free()
                return true
            end
 
            if(getPlayerFreeCap(cid) < getItemWeightById(buy:getDataInt("item_id"), buy:getDataInt("count")))then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You try to buy a " .. buy:getDataString("item_name") .. ". It weight " .. getItemWeightById(buy:getDataInt("item_id"), buy:getDataInt("count")) .. " cap oz. and you have only " .. getPlayerFreeCap(cid) .. " oz. free capacity. Put some items to depot and try again.")
                buy:free()
                return true
            end
 
            if(isItemStackable((buy:getDataString("item_id")))) then
                doPlayerAddItem(cid, buy:getDataString("item_id"), buy:getDataInt("count"))
            else
                for i = 1, buy:getDataInt("count") do
                    doPlayerAddItem(cid, buy:getDataString("item_id"), 1)
                end
            end
            doPlayerSetBalance(cid, getPlayerBalance(cid) - buy:getDataInt("cost"))
            updatePlayerBalance(getCreatureName(cid), getPlayerBalance(cid))
            
            db.executeQuery("DELETE FROM `auction_system` WHERE `id` = " .. t[2] .. ";")
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You bought " .. buy:getDataInt("count") .. " ".. buy:getDataString("item_name") .. " for " .. buy:getDataInt("cost") .. " gps!")
           
            local tid = getPlayerByGUID(buy:getDataInt("player"))
            if(isPlayer(tid)) then
                doPlayerSetBalance(tid, getPlayerBalance(tid) + buy:getDataInt("cost"))
            else
                db.executeQuery("UPDATE `players` SET `balance` = `balance` + " .. buy:getDataInt("cost") .. " WHERE `id` = " .. buy:getDataInt("player") .. ";")
            end
 
            buy:free()
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
        end
    end
 
    if(t[1] == "remove") then
        if((not tonumber(t[2]))) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
            return true
        end
 
        if(config.SendOffersOnlyInPZ) then
            if(not getTilePzInfo(getPlayerPosition(cid))) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must be in PZ area when you remove offerts from database.")
                return true
            end
        end
 
        local delete = db.getResult("SELECT * FROM `auction_system` WHERE `id` = " .. (tonumber(t[2])) .. ";")
        if(delete:getID() ~= -1) then
            if(getPlayerGUID(cid) == delete:getDataInt("player")) then
                db.executeQuery("DELETE FROM `auction_system` WHERE `id` = " .. t[2] .. ";")
                if(isItemStackable(delete:getDataString("item_id"))) then
                    doPlayerAddItem(cid, delete:getDataString("item_id"), delete:getDataInt("count"))
                else
                    for i = 1, delete:getDataInt("count") do
                        doPlayerAddItem(cid, delete:getDataString("item_id"), 1)
                    end
                end
 
                doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Your offert has been deleted from offerts database.")
            else
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This is not your offert!")
            end
           
            delete:free()
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
        end
    end
    return true
end
 
Last edited:
OP
samandriel

samandriel

Active Member
Joined
Oct 19, 2016
Messages
223
Solutions
1
Reaction score
45
holy fuck, your function is exactly like mine on line 18 and when i try to put something on market enchanted like this:

Code:
01:20 You see a [RARE] jagged sword (Atk:24, Def:23).
It weighs 10.00 oz.
Position: [X: 1025] [Y: 1016] [Z: 7].

prints this:
Code:
[1:20:24.046] Darkness has logged in.
8602
1
jagged sword
jagged sword
starts getRarity
rarity:

value:
0
ends getRarity
0
 
OP
samandriel

samandriel

Active Member
Joined
Oct 19, 2016
Messages
223
Solutions
1
Reaction score
45
depends on how your script is using the [rare] code.
Check your onlook to see how it generates it and use that function.

how to?

Code:
function onLook(cid, thing, position, lookDistance)
    print(thing)

???

i tried on look:
Code:
21:20 You see a [RARE] jagged sword (Atk:24, Def:23). It weighs 10.00 oz.

but it don't print anything on console
 

Leesne

Advanced OT User
Joined
Dec 4, 2017
Messages
298
Solutions
33
Reaction score
154
Location
UK
GitHub
Leesneaks
Where is the code that generates [rare]?
If you dont' want to post the full code here share it in PM.
Post your whole onLook and the system you use for the rare or a link to its topic if its on here.
 
OP
samandriel

samandriel

Active Member
Joined
Oct 19, 2016
Messages
223
Solutions
1
Reaction score
45
Where is the code that generates [rare]?
If you dont' want to post the full code here share it in PM.
Post your whole onLook and the system you use for the rare or a link to its topic if its on here.

My bad, i did think that when u make a onlook was to thebug the code
I don't like to hide codes as i remember i take this code from this forum

Here you are:
data/lib/upgradesystem.lua
Code:
--PERFECT UPGRADE SYSTEM
UpgradeHandler = {
    nameLv = {
        [1] = "[RARE]",
        [2] = "[EPIC]",
        [3] = "[LEGENDARY]"
    },
    levels = {
          [1] = {50, false, false},
          [2] = {30, false, false},
          [3] = {15, true, true}
    },
    broadcast = 3,
    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 = 10, 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
 
OP
samandriel

samandriel

Active Member
Joined
Oct 19, 2016
Messages
223
Solutions
1
Reaction score
45
it that getItemAttribute ?

how to use?

i tried:
Code:
        local enchant = getItemAttribute(item.uid, "name")
        print(enchant)

but prints errors on line 123 -> local enchant = getItemAttribute(item.uid, "name")
Code:
8602
1
jagged sword
jagged sword

[1:59:36.290] [Error - TalkAction Interface] 
[1:59:36.290] data/talkactions/scripts/auctionsystem.lua:onSay
[1:59:36.290] Description: 
[1:59:36.290] data/talkactions/scripts/auctionsystem.lua:123: attempt to index local 'item' (a number value)
[1:59:36.290] stack traceback:
[1:59:36.291]     data/talkactions/scripts/auctionsystem.lua:123: in function <data/talkactions/scripts/auctionsystem.lua:34>
 

Leesne

Advanced OT User
Joined
Dec 4, 2017
Messages
298
Solutions
33
Reaction score
154
Location
UK
GitHub
Leesneaks
I don't have a 0.x server setup.

But a quick test on 1.x and this seemed to work.
On my player:eek:nlook

Lua:
function Player:onLook(thing, position, distance)
  tmp = getItemAttribute(thing.uid, "name")
  doItemSetAttribute(thing.uid, "name", tmp .. "test" )
  self:sendTextMessage(MESSAGE_INFO_DESCR, getItemAttribute(thing.uid, "name"))
end


15:31 giant swordtest
15:31 You see a giant swordtest (Atk:46, Def:22).
15:31 giant swordtesttest
15:31 You see a giant swordtesttest (Atk:46, Def:22).

looking at your previous code.
local item = getPlayerSlotItem(cid, CONST_SLOT_AMMO).itemid
so item is the itemid and not the item so won't have a unique id.

local item = getPlayerSlotItem(cid, CONST_SLOT_AMMO)
local item_id = item.itemid

edit the rest of the code accordingly.
 
OP
samandriel

samandriel

Active Member
Joined
Oct 19, 2016
Messages
223
Solutions
1
Reaction score
45
I don't have a 0.x server setup.

But a quick test on 1.x and this seemed to work.
On my player:eek:nlook

Lua:
function Player:onLook(thing, position, distance)
  tmp = getItemAttribute(thing.uid, "name")
  doItemSetAttribute(thing.uid, "name", tmp .. "test" )
  self:sendTextMessage(MESSAGE_INFO_DESCR, getItemAttribute(thing.uid, "name"))
end


15:31 giant swordtest
15:31 You see a giant swordtest (Atk:46, Def:22).
15:31 giant swordtesttest
15:31 You see a giant swordtesttest (Atk:46, Def:22).

looking at your previous code.
local item = getPlayerSlotItem(cid, CONST_SLOT_AMMO).itemid
so item is the itemid and not the item so won't have a unique id.

local item = getPlayerSlotItem(cid, CONST_SLOT_AMMO)
local item_id = item.itemid

edit the rest of the code accordingly.


Thats what are u mean?

Code:
--[[
Offline player to player item trader (Auction System) by vDk
Script version: 1.2a [ -- FIXED CLONE ITEMS BUG -- ]
]]--
local config = {
    levelRequiredToAdd = 5,
    SendOffersOnlyInPZ = true,
    blocked_items = {2165, 2152, 2148, 2160, 2166, 2167, 2168, 2169, 2202, 2203, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212, 2213, 2214, 2215, 2343, 2433, 2640, 6132, 6300, 6301, 9932, 9933, 6093, 2123}
}

local function updatePlayerBalance(name, value)
    db.query('UPDATE players SET balance=' .. value .. ' WHERE name=' .. db.escapeString(name) .. ' LIMIT 1')
end

function getRarity(str)
    print("starts getRarity")
    local value = 0
    local n = str:match("%[(.-)%]")
    print("rarity:")
    print(n)
    if n == "RARE" then
       value = 1
    elseif n == "EPIC" then
       value = 2
    elseif n == "LEGENDARY" then
       value = 3
    end
    print("value:")
    print(value)
    print("ends getRarity")
    return value
end
 
function onSay(cid, words, param, channel)
    if(param == '') then
        local msg = "Market:\n\n/market buy, ID\n/market remove, ID\n/market add, ItemPrice\n\nMore information look in us website!"
        doPlayerPopupFYI(cid, msg)
        return true
    end

    local maxOffersPerPlayer = math.floor(getPlayerLevel(cid) / 5)
    local pricePerOffer = 500
 
    local t = string.explode(param, ",")
    if(t[1] == "add") then
        local Item_Price = t[2]

        if(not Item_Price) then
            local msg = "/market add, ItemPrice"
            doPlayerPopupFYI(cid, msg)
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, msg)
            return true
        end
 
        if(not tonumber(Item_Price)) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't set valid price or items count.")
            return true
        end
 
        if(string.len(Item_Price) > 7) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This price or item count is too high.")
            return true
        end

        if getPlayerSlotItem(cid, CONST_SLOT_AMMO).itemid == 0 then
            return doPlayerSendCancel(cid, "You have no item in your equipament arrow slot to sell!")
        end
        
        local item = getPlayerSlotItem(cid, CONST_SLOT_AMMO).itemid
        print(item)

        local Item_Count = getPlayerSlotItem(cid, CONST_SLOT_AMMO).type
        if(tonumber(Item_Count) < 1) then
            Item_Count = 1
        end
        print(Item_Count)
        local Item_Name = getItemNameById(item)
        print(Item_Name)
 
        if(getPlayerLevel(cid) < config.levelRequiredToAdd) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't have required (" .. config.levelRequiredToAdd .. ") level.")
            return true
        end
 
        if(isInArray(config.blocked_items, item)) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This item is blocked.")
            return true
        end
 
        local check = db.getResult("SELECT `id` FROM `auction_system` WHERE `player` = " .. getPlayerGUID(cid) .. ";")
        if(check:getID() == -1) then
        elseif(check:getRows(true) >= maxOffersPerPlayer) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry you can't add more offers (max for your level: " .. maxOffersPerPlayer .. ")")
            return true
        end
 
        if(config.SendOffersOnlyInPZ) then
            if(not getTilePzInfo(getPlayerPosition(cid))) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must be in PZ area when you add offert to database.")
                return true
            end
        end
 
        if((tonumber(Item_Price) < 1)) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You have to type a number higher than 0.")
            return true
        end

        if(getPlayerBalance(cid) < 500) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You need to have 500GPs in your bank balance to make a offer.")
            return true
        end

        -- check if it is enchanted
        -- check if before name there is:
        -- [RARE] itemname
        -- [EPIC] itemname
        -- [LEGENDARY] itemname

        local itemname = Item_Name
        print(itemname)

        local item_id = item.itemid
        local enchant = getItemAttribute(item_id, "name")
        print(enchant)

        local test = getRarity(itemname)
        print(test)

        doPlayerSetBalance(cid, getPlayerBalance(cid) - 500)
        updatePlayerBalance(getCreatureByName(cid), getPlayerBalance(cid))
 
        local itemcount, costgp = math.floor(Item_Count), math.floor(Item_Price)
        doPlayerRemoveItem(cid, item, itemcount)
        db.executeQuery("INSERT INTO `auction_system` (`player`, `item_name`, `item_id`, `count`, `cost`, `date`) VALUES (" .. getPlayerGUID(cid) .. ", \"" .. Item_Name .. "\", " .. getItemIdByName(Item_Name) .. ", " .. itemcount .. ", " .. costgp ..", " .. os.time() .. ")")
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You successfully add " .. itemcount .." " .. Item_Name .." for " .. costgp .. " gps to offerts database.")
    end
 
    if(t[1] == "buy") then
        if(not tonumber(t[2])) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
            return true
        end
 
        local buy = db.getResult("SELECT * FROM `auction_system` WHERE `id` = " .. (tonumber(t[2])) .. ";")
        if(buy:getID() ~= -1) then
            if (getPlayerBalance(cid) < buy:getDataInt("cost")) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't have enoguh Bank Balance.")
                buy:free()
                return true
            end
 
            if(getPlayerName(cid) == getPlayerNameByGUID(buy:getDataInt("player"))) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, you can't buy your own items.")
                buy:free()
                return true
            end
 
            if(getPlayerFreeCap(cid) < getItemWeightById(buy:getDataInt("item_id"), buy:getDataInt("count")))then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You try to buy a " .. buy:getDataString("item_name") .. ". It weight " .. getItemWeightById(buy:getDataInt("item_id"), buy:getDataInt("count")) .. " cap oz. and you have only " .. getPlayerFreeCap(cid) .. " oz. free capacity. Put some items to depot and try again.")
                buy:free()
                return true
            end
 
            if(isItemStackable((buy:getDataString("item_id")))) then
                doPlayerAddItem(cid, buy:getDataString("item_id"), buy:getDataInt("count"))
            else
                for i = 1, buy:getDataInt("count") do
                    doPlayerAddItem(cid, buy:getDataString("item_id"), 1)
                end
            end
            doPlayerSetBalance(cid, getPlayerBalance(cid) - buy:getDataInt("cost"))
            updatePlayerBalance(getCreatureName(cid), getPlayerBalance(cid))
            
            db.executeQuery("DELETE FROM `auction_system` WHERE `id` = " .. t[2] .. ";")
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You bought " .. buy:getDataInt("count") .. " ".. buy:getDataString("item_name") .. " for " .. buy:getDataInt("cost") .. " gps!")
           
            local tid = getPlayerByGUID(buy:getDataInt("player"))
            if(isPlayer(tid)) then
                doPlayerSetBalance(tid, getPlayerBalance(tid) + buy:getDataInt("cost"))
            else
                db.executeQuery("UPDATE `players` SET `balance` = `balance` + " .. buy:getDataInt("cost") .. " WHERE `id` = " .. buy:getDataInt("player") .. ";")
            end
 
            buy:free()
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
        end
    end
 
    if(t[1] == "remove") then
        if((not tonumber(t[2]))) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
            return true
        end
 
        if(config.SendOffersOnlyInPZ) then
            if(not getTilePzInfo(getPlayerPosition(cid))) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must be in PZ area when you remove offerts from database.")
                return true
            end
        end
 
        local delete = db.getResult("SELECT * FROM `auction_system` WHERE `id` = " .. (tonumber(t[2])) .. ";")
        if(delete:getID() ~= -1) then
            if(getPlayerGUID(cid) == delete:getDataInt("player")) then
                db.executeQuery("DELETE FROM `auction_system` WHERE `id` = " .. t[2] .. ";")
                if(isItemStackable(delete:getDataString("item_id"))) then
                    doPlayerAddItem(cid, delete:getDataString("item_id"), delete:getDataInt("count"))
                else
                    for i = 1, delete:getDataInt("count") do
                        doPlayerAddItem(cid, delete:getDataString("item_id"), 1)
                    end
                end
 
                doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Your offert has been deleted from offerts database.")
            else
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This is not your offert!")
            end
           
            delete:free()
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
        end
    end
    return true
end


cause result this errors:
Code:
8602
1
jagged sword
jagged sword

[4:14:12.176] [Error - TalkAction Interface] 
[4:14:12.176] data/talkactions/scripts/auctionsystem.lua:onSay
[4:14:12.177] Description: 
[4:14:12.177] data/talkactions/scripts/auctionsystem.lua:123: attempt to index local 'item' (a number value)
[4:14:12.177] stack traceback:
[4:14:12.177]     data/talkactions/scripts/auctionsystem.lua:123: in function <data/talkactions/scripts/auctionsystem.lua:34>
 

Leesne

Advanced OT User
Joined
Dec 4, 2017
Messages
298
Solutions
33
Reaction score
154
Location
UK
GitHub
Leesneaks
No, what you are doing is getting the item ID, this is the same across all items in game.
getPlayerSlotItem returns the actual ITEM for that specific item which is where the name will be.

Reread my last post,
you are still using
Lua:
local item = getPlayerSlotItem(cid, CONST_SLOT_AMMO).itemid
print(item)

and didn't make the suggestions I made.
You need the actual ITEM and uniqueID not the itemid as this is standard every sword of valor is 2400, but each sword of valor will have a unique id to tell it apart from the other items created in game..
 

Lessaire

Omniscient Hypervisor
Premium User
Joined
Dec 29, 2009
Messages
1,265
Solutions
46
Reaction score
436
Location
Oregon
Ok, it seems like this should spit out the altered string:

getItemAttribute(ITEMUID, "name")

And this chunk is what the library uses to determine the current level

Lua:
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

Which we can bring out into plain-land like so:


Lua:
function itemExtractNameLevel(item_uid)
    local nameLv = {
        [1] = "[RARE]",
        [2] = "[EPIC]",
        [3] = "[LEGENDARY]",
    }

    local name = getItemAttribute(item_uid, "name")
    local level = 0
    for lvl, flourish in ipairs(nameLv) do
        if name:find(flourish) then
            level = lvl
        end
    end
    return name, lvl, flourish
end
 
OP
samandriel

samandriel

Active Member
Joined
Oct 19, 2016
Messages
223
Solutions
1
Reaction score
45
it was what u guys mean? @Leesne @Lessaire ?

Code:
        local itemUniqueID = getPlayerSlotItem(cid, CONST_SLOT_AMMO).uniqueID
        print(itemUniqueID)


Code:
        local rarity = itemExtractNameLevel(itemUniqueID)
        print(rarity)

i got this errors:
Code:
[code]
8602

1
jagged sword
jagged sword

[20:20:51.825] [Error - TalkAction Interface] 
[20:20:51.825] data/talkactions/scripts/auctionsystem.lua:onSay
[20:20:51.826] Description: 
[20:20:51.826] (luaGetItemAttribute) Item not found

[20:20:51.826] [Error - TalkAction Interface] 
[20:20:51.826] data/talkactions/scripts/auctionsystem.lua:onSay
[20:20:51.826] Description: 
[20:20:51.826] data/talkactions/scripts/auctionsystem.lua:25: attempt to index local 'name' (a nil value)
[20:20:51.826] stack traceback:
[20:20:51.826]     data/talkactions/scripts/auctionsystem.lua:25: in function 'itemExtractNameLevel'
[20:20:51.826]     data/talkactions/scripts/auctionsystem.lua:124: in function <data/talkactions/scripts/auctionsystem.lua:32>

from this full script:
Code:
--[[
Offline player to player item trader (Auction System) by vDk
Script version: 1.2a [ -- FIXED CLONE ITEMS BUG -- ]
]]--
local config = {
    levelRequiredToAdd = 5,
    SendOffersOnlyInPZ = true,
    blocked_items = {2165, 2152, 2148, 2160, 2166, 2167, 2168, 2169, 2202, 2203, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212, 2213, 2214, 2215, 2343, 2433, 2640, 6132, 6300, 6301, 9932, 9933, 6093, 2123}
}

local function updatePlayerBalance(name, value)
    db.query('UPDATE players SET balance=' .. value .. ' WHERE name=' .. db.escapeString(name) .. ' LIMIT 1')
end

function itemExtractNameLevel(item_uid)
    local nameLv = {
        [1] = "[RARE]",
        [2] = "[EPIC]",
        [3] = "[LEGENDARY]",
    }

    local name = getItemAttribute(item_uid, "name")
    local level = 0
    for lvl, flourish in ipairs(nameLv) do
        if name:find(flourish) then
            level = lvl
        end
    end
    return name, lvl, flourish
end

function onSay(cid, words, param, channel)
    if(param == '') then
        local msg = "Market:\n\n/market buy, ID\n/market remove, ID\n/market add, ItemPrice\n\nMore information look in us website!"
        doPlayerPopupFYI(cid, msg)
        return true
    end

    local maxOffersPerPlayer = math.floor(getPlayerLevel(cid) / 5)
    local pricePerOffer = 500
 
    local t = string.explode(param, ",")
    if(t[1] == "add") then
        local Item_Price = t[2]

        if(not Item_Price) then
            local msg = "/market add, ItemPrice"
            doPlayerPopupFYI(cid, msg)
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, msg)
            return true
        end
 
        if(not tonumber(Item_Price)) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't set valid price or items count.")
            return true
        end
 
        if(string.len(Item_Price) > 7) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This price or item count is too high.")
            return true
        end

        if getPlayerSlotItem(cid, CONST_SLOT_AMMO).itemid == 0 then
            return doPlayerSendCancel(cid, "You have no item in your equipament arrow slot to sell!")
        end
        
        local item = getPlayerSlotItem(cid, CONST_SLOT_AMMO).itemid
        print(item)

        local itemUniqueID = getPlayerSlotItem(cid, CONST_SLOT_AMMO).uniqueID
        print(itemUniqueID)

        local Item_Count = getPlayerSlotItem(cid, CONST_SLOT_AMMO).type
        if(tonumber(Item_Count) < 1) then
            Item_Count = 1
        end
        print(Item_Count)
        local Item_Name = getItemNameById(item)
        print(Item_Name)
 
        if(getPlayerLevel(cid) < config.levelRequiredToAdd) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't have required (" .. config.levelRequiredToAdd .. ") level.")
            return true
        end
 
        if(isInArray(config.blocked_items, item)) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This item is blocked.")
            return true
        end
 
        local check = db.getResult("SELECT `id` FROM `auction_system` WHERE `player` = " .. getPlayerGUID(cid) .. ";")
        if(check:getID() == -1) then
        elseif(check:getRows(true) >= maxOffersPerPlayer) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry you can't add more offers (max for your level: " .. maxOffersPerPlayer .. ")")
            return true
        end
 
        if(config.SendOffersOnlyInPZ) then
            if(not getTilePzInfo(getPlayerPosition(cid))) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must be in PZ area when you add offert to database.")
                return true
            end
        end
 
        if((tonumber(Item_Price) < 1)) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You have to type a number higher than 0.")
            return true
        end

        if(getPlayerBalance(cid) < 500) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You need to have 500GPs in your bank balance to make a offer.")
            return true
        end

        -- check if it is enchanted
        -- check if before name there is:
        -- [RARE] itemname
        -- [EPIC] itemname
        -- [LEGENDARY] itemname

        local itemname = Item_Name
        print(itemname)

        local rarity = itemExtractNameLevel(itemUniqueID)
        print(rarity)

        doPlayerSetBalance(cid, getPlayerBalance(cid) - 500)
        updatePlayerBalance(getCreatureByName(cid), getPlayerBalance(cid))
 
        local itemcount, costgp = math.floor(Item_Count), math.floor(Item_Price)
        doPlayerRemoveItem(cid, item, itemcount)
        db.executeQuery("INSERT INTO `auction_system` (`player`, `item_name`, `item_id`, `count`, `cost`, `date`) VALUES (" .. getPlayerGUID(cid) .. ", \"" .. Item_Name .. "\", " .. getItemIdByName(Item_Name) .. ", " .. itemcount .. ", " .. costgp ..", " .. os.time() .. ")")
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You successfully add " .. itemcount .." " .. Item_Name .." for " .. costgp .. " gps to offerts database.")
    end
 
    if(t[1] == "buy") then
        if(not tonumber(t[2])) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
            return true
        end
 
        local buy = db.getResult("SELECT * FROM `auction_system` WHERE `id` = " .. (tonumber(t[2])) .. ";")
        if(buy:getID() ~= -1) then
            if (getPlayerBalance(cid) < buy:getDataInt("cost")) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't have enoguh Bank Balance.")
                buy:free()
                return true
            end
 
            if(getPlayerName(cid) == getPlayerNameByGUID(buy:getDataInt("player"))) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, you can't buy your own items.")
                buy:free()
                return true
            end
 
            if(getPlayerFreeCap(cid) < getItemWeightById(buy:getDataInt("item_id"), buy:getDataInt("count")))then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You try to buy a " .. buy:getDataString("item_name") .. ". It weight " .. getItemWeightById(buy:getDataInt("item_id"), buy:getDataInt("count")) .. " cap oz. and you have only " .. getPlayerFreeCap(cid) .. " oz. free capacity. Put some items to depot and try again.")
                buy:free()
                return true
            end
 
            if(isItemStackable((buy:getDataString("item_id")))) then
                doPlayerAddItem(cid, buy:getDataString("item_id"), buy:getDataInt("count"))
            else
                for i = 1, buy:getDataInt("count") do
                    doPlayerAddItem(cid, buy:getDataString("item_id"), 1)
                end
            end
            doPlayerSetBalance(cid, getPlayerBalance(cid) - buy:getDataInt("cost"))
            updatePlayerBalance(getCreatureName(cid), getPlayerBalance(cid))
            
            db.executeQuery("DELETE FROM `auction_system` WHERE `id` = " .. t[2] .. ";")
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You bought " .. buy:getDataInt("count") .. " ".. buy:getDataString("item_name") .. " for " .. buy:getDataInt("cost") .. " gps!")
           
            local tid = getPlayerByGUID(buy:getDataInt("player"))
            if(isPlayer(tid)) then
                doPlayerSetBalance(tid, getPlayerBalance(tid) + buy:getDataInt("cost"))
            else
                db.executeQuery("UPDATE `players` SET `balance` = `balance` + " .. buy:getDataInt("cost") .. " WHERE `id` = " .. buy:getDataInt("player") .. ";")
            end
 
            buy:free()
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
        end
    end
 
    if(t[1] == "remove") then
        if((not tonumber(t[2]))) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
            return true
        end
 
        if(config.SendOffersOnlyInPZ) then
            if(not getTilePzInfo(getPlayerPosition(cid))) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must be in PZ area when you remove offerts from database.")
                return true
            end
        end
 
        local delete = db.getResult("SELECT * FROM `auction_system` WHERE `id` = " .. (tonumber(t[2])) .. ";")
        if(delete:getID() ~= -1) then
            if(getPlayerGUID(cid) == delete:getDataInt("player")) then
                db.executeQuery("DELETE FROM `auction_system` WHERE `id` = " .. t[2] .. ";")
                if(isItemStackable(delete:getDataString("item_id"))) then
                    doPlayerAddItem(cid, delete:getDataString("item_id"), delete:getDataInt("count"))
                else
                    for i = 1, delete:getDataInt("count") do
                        doPlayerAddItem(cid, delete:getDataString("item_id"), 1)
                    end
                end
 
                doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Your offert has been deleted from offerts database.")
            else
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This is not your offert!")
            end
           
            delete:free()
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
        end
    end
    return true
end
 

Leesne

Advanced OT User
Joined
Dec 4, 2017
Messages
298
Solutions
33
Reaction score
154
Location
UK
GitHub
Leesneaks
I could have wrote the code but I could see you were trying to understand but new to coding, so was trying to guide you to finding your own solution.
Glad you got to the end of it.

"Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime. "
 
Top