• 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 Help with attribute item.

Piifafa

Member
Joined
Apr 16, 2023
Messages
67
Reaction score
16
I recently created an item, which gives attributes to some weapons, but it doesn't work well, you can't see the attribute limit, other than that the person can make unimaginably strong weapons. So I added a breakage system, it's cool but people don't like the way it works, so instead of breakage I would like it to reduce an attribute, that would help me a lot!


Lua:
-- Lista de tipos de armas válidas
local weaponTypes = {WEAPON_SWORD, WEAPON_CLUB, WEAPON_AXE}

-- Configuração dos atributos
local attrConfig = {maxValue = 10}
local attrs = {
    [1] = {code = ITEM_ATTRIBUTE_LIFELEECH, effect = CONST_ME_MAGIC_GREEN},
    [2] = {code = ITEM_ATTRIBUTE_MANALEECH, effect = CONST_ME_MAGIC_BLUE},
    [3] = {code = ITEM_ATTRIBUTE_CRITICAL, effect = CONST_ME_CRITICAL_DAMAGE},
    [4] = {code = ITEM_ATTRIBUTE_ATTACK, effect = CONST_ME_DRAWBLOOD, base = true},
    [5] = {code = ITEM_ATTRIBUTE_DEFENSE, effect = CONST_ME_BLOCKHIT, base = true}
}

-- Efeitos das gemas
local gemsEffect = {
    [5245] = {chance = 40},
    [5246] = {chance = 50},
    [5247] = {chance = 60},
    [5248] = {chance = 80},
    [5249] = {chance = 90},
}

-- Função para manipular o uso da gema
function onUse(player, item, fromPosition, target, toPosition)
    -- Verifica se o alvo é um item
    if not target or not target:isItem() then
        return false
    end

    local itemTarget = target:getId()
    local itemType = ItemType(itemTarget)
    local weaponType = itemType:getWeaponType()

    -- Verifica se o tipo de arma é válido
    if not table.contains(weaponTypes, weaponType) then
        return false
    end

    -- Obtém o número de encantamentos do item
    local attrEnchantCount = target:getAttribute(ITEM_ATTRIBUTE_CUSTOM_MAXATTR) or 0

    -- Verifica se o item já foi encantado 10 vezes
    if attrEnchantCount >= 10 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "This equipment is already enchanted 10 times.")
        return false
    end

    -- Remove a gema usada
    if item:remove(1) then
        local currentGem = gemsEffect[item:getId()]
        if math.random(1, 100) <= math.min(math.max(10, currentGem.chance)) then
            -- Seleciona um atributo aleatório
            local randomAttr = math.random(1, #attrs)
            local getCurrentAttr = target:getAttribute(attrs[randomAttr].code) or 0

            -- Incrementa o valor do atributo
            if getCurrentAttr == 0 then
                getCurrentAttr = attrs[randomAttr].base and (attrs[randomAttr].code == ITEM_ATTRIBUTE_ATTACK and itemType:getAttack() + 1 or itemType:getDefense() + 1) or 1
            else
                getCurrentAttr = getCurrentAttr + 1
            end

            -- Verifica se a gema falhou
            if math.random(1, 100) <= math.min(math.max(10, (1 + attrEnchantCount))) then
                player:say('Crack..!', TALKTYPE_MONSTER_SAY)
                target:remove()
            else
                -- Aplica o encantamento e atualiza os atributos
                target:setAttribute(attrs[randomAttr].code, getCurrentAttr)
                target:setAttribute(ITEM_ATTRIBUTE_CUSTOM_MAXATTR, (attrEnchantCount + 1))
                target:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, 'This equipment is enchanted.')
                target:getPosition():sendMagicEffect(attrs[randomAttr].effect)
            end
        end
    end

    return true
end
 
Back
Top