• 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 Include extra text in an item description

CastorFlynn

Member
Joined
Aug 29, 2021
Messages
88
Reaction score
8
The default script overwrites the item description with the buyer's name. How do I keep the original description and include the buyer's name with it?
For example:
You see a helmet of the ancients (Arm:8).
It weighs 27.60 oz.
The gem of the helmet is burned out and should be replaced. (Bought by John)
Lua:
function addItem(player, transactionId, itemId, itemCount, offerId)
    local _itemId = tonumber(itemId)
    local _itemCount = tonumber(itemCount)
    local _offerId = tonumber(offerId)
        
    local playerName = player:getName()
    
    local deliver = deliverItem(player, _itemId, _itemCount)
    local itemName = ItemType(_itemId):getName()
    
        if deliver.item then
            deliver.item:setAttribute('description', ('Bought by %s.'):format(playerName))
        end
end
 
Solution
Lua:
local oldDescription = deliver.item:getAttribute("description") == "" and ItemType(itemId):getDescription() or deliver.item:getAttribute("description")
local newDescription = oldDescription .." (Bought by ".. playerName ..")"
deliver.item:setAttribute('description', newDescription)
if an item still has it's original description and it wasn't manipulated through setAttribute then getAttribute will just return an empty string, you have to use ItemType(itemid):getDescription() to get the original description then
Not sure about function names, but you can probably find out
Lua:
function addItem(player, transactionId, itemId, itemCount, offerId)
    local _itemId = tonumber(itemId)
    local _itemCount = tonumber(itemCount)
    local _offerId = tonumber(offerId)
        
    local playerName = player:getName()
    
    local deliver = deliverItem(player, _itemId, _itemCount)
    local itemName = ItemType(_itemId):getName()
    
    if deliver.item then
        local currentDesc = deliver.item:getAttribute('description');
        local boughtBy = '(Bought by %s)':format(playerName);
        deliver.item:setAttribute('description', currentDesc .. ' ' .. boughtBy);
    end
end
 
Not sure about function names, but you can probably find out
Lua:
function addItem(player, transactionId, itemId, itemCount, offerId)
    local _itemId = tonumber(itemId)
    local _itemCount = tonumber(itemCount)
    local _offerId = tonumber(offerId)
       
    local playerName = player:getName()
   
    local deliver = deliverItem(player, _itemId, _itemCount)
    local itemName = ItemType(_itemId):getName()
   
    if deliver.item then
        local currentDesc = deliver.item:getAttribute('description');
        local boughtBy = '(Bought by %s)':format(playerName);
        deliver.item:setAttribute('description', currentDesc .. ' ' .. boughtBy);
    end
end
Thanks for answering.

There was an error on one line:
Code:
/data/scripts/globalevents/shop.lua:150: unexpected symbol near ':'

Line 150:
Lua:
local boughtBy = '(Bought by %s)':format(playerName);
 
Apparently the code is not finding the original description to insert into the currentdesc variable.

If I use
Lua:
deliver.item:setAttribute('description', 'Hi' .. currentDesc)
the description appears only as: Hi
but if use
Lua:
deliver.item:setAttribute('description', 'Hi' .. boughtBy)
appears as: Hi Bought by player

So I believe there is something wrong with returning the original description for currentDesc variation.
 
Lua:
local oldDescription = deliver.item:getAttribute("description") == "" and ItemType(itemId):getDescription() or deliver.item:getAttribute("description")
local newDescription = oldDescription .." (Bought by ".. playerName ..")"
deliver.item:setAttribute('description', newDescription)
if an item still has it's original description and it wasn't manipulated through setAttribute then getAttribute will just return an empty string, you have to use ItemType(itemid):getDescription() to get the original description then
 
Solution
Lua:
local oldDescription = deliver.item:getAttribute("description") == "" and ItemType(itemId):getDescription() or deliver.item:getAttribute("description")
local newDescription = oldDescription .." (Bought by ".. playerName ..")"
deliver.item:setAttribute('description', newDescription)
if an item still has it's original description and it wasn't manipulated through setAttribute then getAttribute will just return an empty string, you have to use ItemType(itemid):getDescription() to get the original description then

That worked! Thanks!
 
Lua:
local oldDescription = deliver.item:getAttribute("description") == "" and ItemType(itemId):getDescription() or deliver.item:getAttribute("description")
local newDescription = oldDescription .." (Bought by ".. playerName ..")"
deliver.item:setAttribute('description', newDescription)
if an item still has it's original description and it wasn't manipulated through setAttribute then getAttribute will just return an empty string, you have to use ItemType(itemid):getDescription() to get the original description then
Boku no hero
 
Back
Top