• 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 1.3] Script to sacrifice 3 of the same item and get a new one

Sunset

Member
Joined
Jun 3, 2009
Messages
26
Reaction score
8
Location
Uruguay
Hey! I was messing up a 10.98 RL map with remere's map editor when I found these. After some time and some nasty edits I came up with some cool weapon concepts but I'm missing something: the possibility to upgrade from tier to tier.

My idea is to have a script to combine three of these awesome weapons in a shrine to obtain one of the next tier: like this.
The script would require a player to place three of the same item (same id and everything) in each of the shrines, then detect which item it is, remove it and add one item of the next "tier" in the middle shrine.

Some cool additions that would be awesome for the script to have:
  • The possibility to combine more than one type of weapon and more than one tier, always with three of the same kind.
  • A chance for the process to fail, destroying all or some of the items in the process.
This is a lot to ask, but all I really need is the initial thing to work, the rest would be just pure cool additions to have a whole upgrade system. I think the initial concept can be and easy thing to make for someone who knows lua scripting.

In addition, this is the id list for the red weapons as an example. I can provide the rest if needed, or any additional information that might help the production of this script.

Anyways, thanks A LOT in advance and let me know if there is anything I can do to help.
 
Solution
Hi,
This is a action, so should be your LEVER action. Set a actionId to it and properly register in actions.xml and your map.
I think this should work. Just setup with your own positions and itens ID. I tryed to comment the code to be more easy to understand.
Lua:
--table structure:
--[ItemToUpgradeId] = {result = NewItemId, chance = chance to sucess}
local itensUpgrade = {
    [5000] = {result = 5001, chance = 60},
    [5001] = {result = 5002, chance = 40},
    [5002] = {result = 5003, 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(99, 100...
Hi,
This is a action, so should be your LEVER action. Set a actionId to it and properly register in actions.xml and your map.
I think this should work. Just setup with your own positions and itens ID. I tryed to comment the code to be more easy to understand.
Lua:
--table structure:
--[ItemToUpgradeId] = {result = NewItemId, chance = chance to sucess}
local itensUpgrade = {
    [5000] = {result = 5001, chance = 60},
    [5001] = {result = 5002, chance = 40},
    [5002] = {result = 5003, 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(99, 100, 7),
    [2] = Tile(98, 100, 7),
    [3] = Tile(97, 100, 7),
}

local newItemPos = Tile(100, 100, 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:getTopTopItem() --get top item of the position
        
        --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 = 0--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
Let me know if work. I will try help if don't.
 
Solution
Hey, can't belive you actually did it! This is awesome man, thank you so much!

I couldn't get it to work yet but not because of the script, I belive:
Annotation 2020-04-18 021634.png
This is the error I get...
But as soon as I saw it I knew it wasn't the script, because I got this problem with other scripts before:Annotation 2020-04-18 021922.png

I couldn't find a fix for this yet and I'm quite lost. I tink it may be a lib problem but I'm like really new on this whole script and code subject (usually I just do mapping, monsters and such).

The script is all I needed though so while I try to fix this, I will try to test it on another better working server later and let you know if it works... I think this should work like a charm though, for which I thank you very very much! (Im trying to find out how to give you rep).

I'm edditing this response to add one more image of a different error im getting with the script on console, just to add more infomartion for a possible fix later:
new_Error.png
 

Attachments

  • Annotation 2020-04-18 022036.png
    Annotation 2020-04-18 022036.png
    12.2 KB · Views: 3 · VirusTotal
Last edited:
Hey, can't belive you actually did it! This is awesome man, thank you so much!

I couldn't get it to work yet but not because of the script, I belive:
View attachment 44423
This is the error I get...
But as soon as I saw it I knew it wasn't the script, because I got this problem with other scripts before:View attachment 44424

I couldn't find a fix for this yet and I'm quite lost. I tink it may be a lib problem but I'm like really new on this whole script and code subject (usually I just do mapping, monsters and such).

The script is all I needed though so while I try to fix this, I will try to test it on another better working server later and let you know if it works... I think this should work like a charm though, for which I thank you very very much! (Im trying to find out how to give you rep).

I'm edditing this response to add one more image of a different error im getting with the script on console, just to add more infomartion for a possible fix later:
View attachment 44428
Hi,
Can you send a photo of your setup? Like you did in first post.
The shrines position, the itens above and etc? Change the code for this one, and send me what appears in console.

Lua:
--table structure:
--[ItemToUpgradeId] = {result = NewItemId, chance = chance to sucess}
local itensUpgrade = {
    [5000] = {result = 5001, chance = 60},
    [5001] = {result = 5002, chance = 40},
    [5002] = {result = 5003, 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(99, 100, 7),
    [2] = Tile(98, 100, 7),
    [3] = Tile(97, 100, 7),
}

local newItemPos = Tile(100, 100, 7)

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    print("Start Script")
    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
        print("Looking in pos: "..tempPos:getPosition())
        local topItem = tempPos:getTopTopItem() --get top item of the position
        
        --Top item don't exist
        if not topItem then
            return true
        end
        
        print("Found TopItem with id: "..topItem:getId())
        
        --Top item in some sacrifice position don't is a upgradable item
        if not itensUpgrade[topItem:getId()] then
            print("Don't exist config for top item. Exiting.")
            return true
        end
        
        --for first loop occurrence, set valid item to upgrade as target item
        if targetItem == -1 then
            print("Target item is: "..topItem:getId())
            targetItem = topItem:getId()
        end
        
        --some sacrifice item is diferent of other
        if targetItem ~= topItem:getId() then
            print("targetItem "..targetItem.." ~= id of topItem"..topItem:getId()..". Exiting.")
            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 = 0--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
 
Hey! I used both the first script and the last, to no avail.
This is the setup I'm using:
1.png2.png

This is the error I'm getting:
error_1.png

With this code:
Lua:
--table structure:
--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(158, 383, 7),
    [2] = Tile(159, 382, 7),
    [3] = Tile(160, 383, 7)
}
local newItemPos = Tile(159, 382, 7)
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem = -1 --initialize a variable to hold target item id
    local itensToRemove = {3} --initialize a table to hold all itens userData to remove
    for _, tempPos in ipairs(sacrificePos) do --for each sacrifice position
        local topItem = { tempPos:getTopTopItem() } --get top item of the position
      
        --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 = 0--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(CONST_ME_FIREWORK_RED)
    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

And this is the error I get...error_2.png

With this code:
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(158, 383, 7),
    [2] = Tile(159, 382, 7),
    [3] = Tile(160, 383, 7)
}

local newItemPos = Tile(159, 382, 7)

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    print("Start Script")
    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
        print("Looking in pos: "..tempPos:getPosition())
        local topItem = tempPos:getTopTopItem() --get top item of the position
      
        --Top item don't exist
        if not topItem then
            return true
        end
      
        print("Found TopItem with id: "..topItem:getId())
      
        --Top item in some sacrifice position don't is a upgradable item
        if not itensUpgrade[topItem:getId()] then
            print("Don't exist config for top item. Exiting.")
            return true
        end
      
        --for first loop occurrence, set valid item to upgrade as target item
        if targetItem == -1 then
            print("Target item is: "..topItem:getId())
            targetItem = topItem:getId()
        end
      
        --some sacrifice item is diferent of other
        if targetItem ~= topItem:getId() then
            print("targetItem "..targetItem.." ~= id of topItem"..topItem:getId()..". Exiting.")
            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

By the way, to run this script I used the lastest TFS distribution in a clean server.
 
Hey! I used both the first script and the last, to no avail.
This is the setup I'm using:
View attachment 44462View attachment 44463

This is the error I'm getting:
View attachment 44465

With this code:
Lua:
--table structure:
--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(158, 383, 7),
    [2] = Tile(159, 382, 7),
    [3] = Tile(160, 383, 7)
}
local newItemPos = Tile(159, 382, 7)
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem = -1 --initialize a variable to hold target item id
    local itensToRemove = {3} --initialize a table to hold all itens userData to remove
    for _, tempPos in ipairs(sacrificePos) do --for each sacrifice position
        local topItem = { tempPos:getTopTopItem() } --get top item of the position
     
        --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 = 0--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(CONST_ME_FIREWORK_RED)
    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

And this is the error I get...View attachment 44466

With this code:
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(158, 383, 7),
    [2] = Tile(159, 382, 7),
    [3] = Tile(160, 383, 7)
}

local newItemPos = Tile(159, 382, 7)

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    print("Start Script")
    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
        print("Looking in pos: "..tempPos:getPosition())
        local topItem = tempPos:getTopTopItem() --get top item of the position
     
        --Top item don't exist
        if not topItem then
            return true
        end
     
        print("Found TopItem with id: "..topItem:getId())
     
        --Top item in some sacrifice position don't is a upgradable item
        if not itensUpgrade[topItem:getId()] then
            print("Don't exist config for top item. Exiting.")
            return true
        end
     
        --for first loop occurrence, set valid item to upgrade as target item
        if targetItem == -1 then
            print("Target item is: "..topItem:getId())
            targetItem = topItem:getId()
        end
     
        --some sacrifice item is diferent of other
        if targetItem ~= topItem:getId() then
            print("targetItem "..targetItem.." ~= id of topItem"..topItem:getId()..". Exiting.")
            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

By the way, to run this script I used the lastest TFS distribution in a clean server.
Sorry for the delay, as you don't quoted me, i don't recived the notification.
Try:

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(158, 383, 7),
    [2] = Tile(159, 382, 7),
    [3] = Tile(160, 383, 7)
}

local newItemPos = Tile(159, 382, 7)

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    print("Start Script")
    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
        print("Looking in pos: ",tempPos:getPosition().x,tempPos:getPosition().y,tempPos:getPosition().z)
        local topItem = tempPos:getTopTopItem() --get top item of the position
      
        --Top item don't exist
        if not topItem then
            return true
        end
      
        print("Found TopItem with id: "..topItem:getId())
      
        --Top item in some sacrifice position don't is a upgradable item
        if not itensUpgrade[topItem:getId()] then
            print("Don't exist config for top item. Exiting.")
            return true
        end
      
        --for first loop occurrence, set valid item to upgrade as target item
        if targetItem == -1 then
            print("Target item is: "..topItem:getId())
            targetItem = topItem:getId()
        end
      
        --some sacrifice item is diferent of other
        if targetItem ~= topItem:getId() then
            print("targetItem "..targetItem.." ~= id of topItem"..topItem:getId()..". Exiting.")
            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
Send what show in console
 
Hey! Im sorry I didn't quote, I didn't really know how to lol
Thanks for replying!
I loaded the script and this is what happens:
1587529819824.png
This happens after re-opening the server. Now if right after I open the server I reload actions and use the lever the message changes to this:
1587529879634.png
Note: I've used it many times. It seems some of what you changed somewhat worked since you get that output in console instead of a normal error like in the image before this. You are making me thing this might eventually work! :D
Thanks again for this!
 
Sorry for the delay, as you don't quoted me, i don't recived the notification.
Try:

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(158, 383, 7),
    [2] = Tile(159, 382, 7),
    [3] = Tile(160, 383, 7)
}

local newItemPos = Tile(159, 382, 7)

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    print("Start Script")
    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
        print("Looking in pos: ",tempPos:getPosition().x,tempPos:getPosition().y,tempPos:getPosition().z)
        local topItem = tempPos:getTopTopItem() --get top item of the position
     
        --Top item don't exist
        if not topItem then
            return true
        end
     
        print("Found TopItem with id: "..topItem:getId())
     
        --Top item in some sacrifice position don't is a upgradable item
        if not itensUpgrade[topItem:getId()] then
            print("Don't exist config for top item. Exiting.")
            return true
        end
     
        --for first loop occurrence, set valid item to upgrade as target item
        if targetItem == -1 then
            print("Target item is: "..topItem:getId())
            targetItem = topItem:getId()
        end
     
        --some sacrifice item is diferent of other
        if targetItem ~= topItem:getId() then
            print("targetItem "..targetItem.." ~= id of topItem"..topItem:getId()..". Exiting.")
            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
Send what show in console
I realize now I didn't quote you now either so I'm doing it. Better late than never, right? haha
 
I realize now I didn't quote you now either so I'm doing it. Better late than never, right? haha
Hi,
Sorry for the delay, i was without time to do and test it. Now it is working well, i tested here. Just be sure to set the positions correct.

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(95, 129, 7),
    [2] = Tile(93, 129, 7),
    [3] = Tile(94, 128, 7)
}

local newItemPos = Tile(93, 128, 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

Any problem, let me know.
 
Back
Top