• 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 TFS 1.3 Custom Item Attr

Itutorial

Excellent OT User
Joined
Dec 23, 2014
Messages
2,326
Solutions
68
Reaction score
999
I am trying to create a custom item attribute system. The problem is in string.find. It is acting like it finds strings in the items description that are not there.

Lua:
local runes = {2261, 2262}
local items = {7438}

local attributes = {
    [1] = "[DMG1]",
    [2] = "[HP Leech1]",
    [3] = "[MP Leech1]",
    [4] = "[Crit Chance1]",
    [5] = "[Crit Damage1]",
    [6] = "[Speed1]",
    [7] = "[Max HP1]",
    [8] = "[Max MP1]",
    [9] = "[Soul Regen1]",
    [10] = "[DMG Reduce1]",
    [11] = "[Extra EXP1]",
    [12] = "[Loot Bonus1]"
}

local maxAttributes = 3

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not isInArray(runes, item.itemid) then return false end
    if not isInArray(items, target.itemid) then return false end
  
    local ATTR = CustomItemAttributes[item.itemid]
  
    local text = target:getAttribute('description')
    local count = 0
  
    for i = 1, #attributes do
        if string.find(text, attributes[i]) then
            count = count + 1
        end
    end
   -- Problem here --
    if string.find(text, ATTR.name) then
        return player:sendCancelMessage("This attribute is already added to the item.")
    end
   -- Problem here too --
    if count >= maxAttributes then
        return player:sendCancelMessage("This items slots are full. You cannot add anymore attributes.")
    end
  
    text = text.."\n"..ATTR.name.." "..ATTR.value
    item:remove(1)
    target:setAttribute('description', text)
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have added "..ATTR.value.." "..ATTR.desc.." to the item.")
    return true
end

The count (even if the item only has 1 attribute on it says it has counted 12. It also says that the item has the attribute added already even if it doesn't. Is there some bug in string.find that I do not know about?
 
Last edited by a moderator:
string.find returns the position of where the string was found

if you look for c and your string is abcd, it will return 3
 
btw you can store attributes this way:
sword:4;axe:2;fire:5

and use string:split(";") to extract stat boxes and then :split(":") to obtain stat name and value from string

and to make it look good for player, you could wrap them in those [ ] for onLook purposes, eg: string.format("[%s: %s]", attribute, value)
also you can format positive attributes on the fly
Code:
if tonumber(value) and value > 0 then
    value = "+" .. value
end
 
Last edited:
Back
Top