• 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 Transform item

_M4G0_

Well-Known Member
Joined
Feb 6, 2016
Messages
504
Solutions
16
Reaction score
98
Hello,
I found this script, I would like that instead of creating a new item it transforms the item with the same attributes as the previous one.
knight helmet LV1 to knight helmet LV2, for example

knight helmet LV1 XX (Arm:215)
Slots:
Increase 15% max melee.
Increase 15% max melee.
Increase 15% max melee.
Refine [Level: 15, Bônus: +175 Arm].

so the knight helmet LV2 would have the same attribute as level 1
on a specific tile would be the item with enchants and refinements
in two other common items, without refinements or enchantments
1677045909211.png
Lua:
--table structure:
--[ItemToUpgradeId] = {result = NewItemId, chance = chance to sucess}
local itensUpgrade = {
    [25919] = {result = 25920, chance = 60},
    [25920] = {result = 25921, chance = 40},
    [25921] = {result = 25922, chance = 20}
}

--put a index and a tile for position, you can add more, just be sure to config.
--NOTE: this script is for a single altar, don't work with more than one at same time. Need changes or multiples files to more altars.
local sacrificePos = {
    [1] = Tile(1026, 1026, 7),
    [2] = Tile(1024, 1026, 7),
    [3] = Tile(1024, 1027, 7)
}

local newItemPos = Tile(1027, 1026, 7)

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem = -1 --initialize a variable to hold target item id
    local itensToRemove = {} --initialize a table to hold all itens userData to remove
    for _, tempPos in ipairs(sacrificePos) do --for each sacrifice position
        local topItem = tempPos:getTopDownItem() --get top item of the position

        --Top item don't exist
        if not topItem then
            return true
        end

        --Top item in some sacrifice position don't is a upgradable item
        if not itensUpgrade[topItem:getId()] then
            return true
        end

        --for first loop occurrence, set valid item to upgrade as target item
        if targetItem == -1 then
            targetItem = topItem:getId()
        end

        --some sacrifice item is diferent of other
        if targetItem ~= topItem:getId() then
            return true
        end

        table.insert(itensToRemove, topItem)--add the item userData to table
    end

    local randChance = math.random(1, 100)--get a random number
    local effect = 12--initialize a variable to hold effect to be sended to player
    local msg = ""--initialize a variable to hold msg to be sended to player
    local materialCountToRemove = 0--initialize a variable to hold the count of itens that will be removed

    --if the random chance number is lower or equal to chance to sucess
    if randChance <= itensUpgrade[targetItem].chance then
        --sucess
        materialCountToRemove = #itensToRemove --set remove count to ALL itens in table
        msg = "Congratulations, your item has been upgrated to "..ItemType(itensUpgrade[targetItem].result):getName().."." --update msg
        effect = CONST_ME_FIREWORK_RED --update effect
        Game.createItem(itensUpgrade[targetItem].result, 1, newItemPos:getPosition()) --create the new item
    else
        --fail
        materialCountToRemove = math.random(1, #itensToRemove)--get a random chance to remove from 1 to ALL itens
        msg = "You failed to upgrade your item. You've lost "..materialCountToRemove.." "..ItemType(targetItem):getName().."."--update msg
        effect = CONST_ME_POFF--update effect
    end

    --send effect and msg
    player:getPosition():sendMagicEffect(effect)
    player:sendTextMessage(MESSAGE_STATUS_SMALL, msg)

    --remove material itens
    for i = 1, materialCountToRemove do
        --don't know if needed, don't know if when removing the item, it will be removed from table too?
        local tmpItem = itensToRemove[i]
        itensToRemove[i] = 0 --set to 0 the value of i Index, to preserv table size if removed itens is also removed from table
        --itensToRemove[i]:remove()
        tmpItem:remove()
    end
    return true
end
 
Basically the reason nobody is helping you, is because you've asked for something more or less impossible for us to script without more information.

We have no idea how your slots or refine system work.

We can copy an item with all of it's current information, but there is no way to upgrade or downgrade that item, without using your existing custom systems.

----
The way you currently describe it in your first post makes no sense in that regard currently.. because lv2 and lv1 helmet have the same attributes?
"so the knight helmet LV2 would have the same attribute as level 1"

So what is even the point of 'transforming' it to level 2, if it's the same item?

----
Basically there's so many questions.. we have no idea how to help you.

But if you literally just want to make a clone of the item..

Lua:
local newItem = targetItem:clone() -- targetItem found earlier in your script
newItemPos:addItemEx(newItem) -- newItemPos found earlier in your script as well
 
Basically the reason nobody is helping you, is because you've asked for something more or less impossible for us to script without more information.

We have no idea how your slots or refine system work.

We can copy an item with all of it's current information, but there is no way to upgrade or downgrade that item, without using your existing custom systems.

----
The way you currently describe it in your first post makes no sense in that regard currently.. because lv2 and lv1 helmet have the same attributes?
"so the knight helmet LV2 would have the same attribute as level 1"

So what is even the point of 'transforming' it to level 2, if it's the same item?

----
Basically there's so many questions.. we have no idea how to help you.

But if you literally just want to make a clone of the item..

Lua:
local newItem = targetItem:clone() -- targetItem found earlier in your script
newItemPos:addItemEx(newItem) -- newItemPos found earlier in your script as well
Thanks for your answer, seeing it from that side, yes it really doesn't make sense my request, the slots will always be the same the refinement system makes this change in the item, if the helmet lvl 1 refined has Arm 215 and the lvl 2 Arm 225 automatically when the system detects this change of item it will put the correct value for lvl 2.

basically functional that way, an item will transform into another

in this case need 3 items for transform to next item

i have add your change

Code:
Lua Script Error: [Action Interface]
data/actions/scripts/custom/machine.lua:onUse
data/actions/scripts/custom/machine.lua:55: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/actions/scripts/custom/machine.lua:55: in function <data/actions/scripts/custom/machine.lua:19>


Lua:
    if randChance <= itensUpgrade[targetItem].chance then
        --sucess
        materialCountToRemove = #itensToRemove --set remove count to ALL itens in table
        msg = "Congratulations, your item has been upgrated to "..ItemType(itensUpgrade[targetItem].result):getName().."." --update msg
        effect = CONST_ME_FIREWORK_RED --update effect
        local newItem = targetItem:clone() -- targetItem found earlier in your script
        newItemPos:addItemEx(newItem) -- newItemPos found earlier in your script as well
    else
        --fail
        materialCountToRemove = math.random(1, #itensToRemove)--get a random chance to remove from 1 to ALL itens
        msg = "You failed to upgrade your item. You've lost "..materialCountToRemove.." "..ItemType(targetItem):getName().."."--update msg
        effect = CONST_ME_POFF--update effect
    end
thank you for taking your time to do this
 
Last edited:
Basically the reason nobody is helping you, is because you've asked for something more or less impossible for us to script without more information.

We have no idea how your slots or refine system work.

We can copy an item with all of it's current information, but there is no way to upgrade or downgrade that item, without using your existing custom systems.

----
The way you currently describe it in your first post makes no sense in that regard currently.. because lv2 and lv1 helmet have the same attributes?
"so the knight helmet LV2 would have the same attribute as level 1"

So what is even the point of 'transforming' it to level 2, if it's the same item?

----
Basically there's so many questions.. we have no idea how to help you.

But if you literally just want to make a clone of the item..

Lua:
local newItem = targetItem:clone() -- targetItem found earlier in your script
newItemPos:addItemEx(newItem) -- newItemPos found earlier in your script as well

basically is this

Lua:
--table structure:
--[ItemToUpgradeId] = {result = NewItemId, chance = chance to sucess}
local itensUpgrade = {
    [38929] = {result = 38928, chance = 100},
    [25920] = {result = 25921, chance = 40},
    [25921] = {result = 25922, chance = 20}
}

--put a index and a tile for position, you can add more, just be sure to config.
--NOTE: this script is for a single altar, don't work with more than one at same time. Need changes or multiples files to more altars.
local sacrificePos = {
    [1] = Tile(1026, 1026, 7),
    [2] = Tile(1024, 1026, 7)
}

local offer_pos = Position(1024, 1027, 7)

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local tile = Tile(offer_pos)
    local targetItem = -1 --initialize a variable to hold target item id
    local itensToRemove = {} --initialize a table to hold all itens userData to remove
    for _, tempPos in ipairs(sacrificePos) do --for each sacrifice position
        local topItem = tempPos:getTopDownItem() --get top item of the position

        --Top item don't exist
        if not tile and topItem then
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You don't have the right item to offer.")
            return true
        end

        --Top item in some sacrifice position don't is a upgradable item
        if not itensUpgrade[topItem:getId()] then
            return true
        end

        --for first loop occurrence, set valid item to upgrade as target item
        if targetItem == -1 then
            targetItem = topItem:getId()
        end

        --some sacrifice item is diferent of other
        if targetItem ~= topItem:getId() then
            return true
        end

        table.insert(itensToRemove, topItem)--add the item userData to table
    end

    local randChance = math.random(1, 100)--get a random number
    local effect = 12--initialize a variable to hold effect to be sended to player
    local msg = ""--initialize a variable to hold msg to be sended to player
    local materialCountToRemove = 0--initialize a variable to hold the count of itens that will be removed
    local offer = tile:getTopDownItem()

        materialCountToRemove = #itensToRemove --set remove count to ALL itens in table
        msg = "Congratulations, your item has been upgrated to "..ItemType(itensUpgrade[targetItem].result):getName().."." --update msg
        effect = CONST_ME_FIREWORK_RED --update effect
        local newItem = offer:transform(itensUpgrade[targetItem].result) -- targetItem found earlier in your script
        -- player:addItem(newItem) -- newItemPos found earlier in your script as well
 
    --send effect and msg
    player:getPosition():sendMagicEffect(effect)
    player:sendTextMessage(MESSAGE_STATUS_SMALL, msg)

    --remove material itens
    for i = 1, materialCountToRemove do
        --don't know if needed, don't know if when removing the item, it will be removed from table too?
        local tmpItem = itensToRemove[i]
        itensToRemove[i] = 0 --set to 0 the value of i Index, to preserv table size if removed itens is also removed from table
        --itensToRemove[i]:remove()
        tmpItem:remove()
    end
    return true
end

there are some errors, which i don't know how to resolve...

if there is no item in the positions

Code:
Lua Script Error: [Action Interface]
data/actions/scripts/custom/machine.lua:onUse
data/actions/scripts/custom/machine.lua:32: attempt to index local 'topItem' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/actions/scripts/custom/machine.lua:32: in function <data/actions/scripts/custom/machine.lua:18>
 
Back
Top