• 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 About tables..

tokenzz

:thinking:
Joined
Feb 2, 2013
Messages
779
Solutions
2
Reaction score
346
Code:
local config = {
    [9808] = { -- Common Rusty Armor
        [1] = {id = 2464, name = "Chain Armor", chance = 6994},
        [2] = {id = 2483, name = "Scale Armor", chance = 3952},
        [3] = {id = 2465, name = "Brass Armor", chance = 1502},
        [4] = {id = 2463, name = "Plate Armor", chance = 197}
        },                   
    [9809] = { -- Semi-rare Rusty Armor
        [1] = {id = 2483, name = "Scale Armor", chance = 6437},               
        [2] = {id = 2464, name = "Chain Armor", chance = 4606},
        [3] = {id = 2465, name = "Brass Armor", chance = 3029},
        [4] = {id = 2463, name = "Plate Armor", chance = 1559},
        [5] = {id = 2476, name = "Knight Armor", chance = 595},
        [6] = {id = 8891, name = "Paladin Armor", chance = 283},
        [7] = {id = 2487, name = "Crown Armor", chance = 49}
        },             
    [9810] = { -- Rare Rusty Armor
        [1] = {id = 2465, name = "Brass Armor", chance = 6681},
        [2] = {id = 2463, name = "Plate Armor", chance = 3767},
        [3] = {id = 2476, name = "Knight Armor", chance = 1832},
        [4] = {id = 2487, name = "Crown Armor", chance = 177},
        [5] = {id = 8891, name = "Paladin Armor", chance = 31},
        [6] = {id = 2466, name = "Golden Armor", chance = 10}
        },                   
    [9811] = { -- Common Rusty Legs
        [1] = {id = 2648, name = "Chain Legs", chance = 6949},               
        [2] = {id = 2468, name = "Studded Legs", chance = 3692},
        [3] = {id = 2478, name = "Brass Legs", chance = 1307},
        [4] = {id = 2647, name = "Plate Legs", chance = 133}
        },               
    [9812] = { -- Semi-Rare Rusty Legs
        [1] = {id = 2468, name = "Studded Legs", chance = 5962},
        [2] = {id = 2648, name = "Chain Legs", chance = 4037},
        [3] = {id = 2478, name = "Brass Legs", chance = 2174},
        [4] = {id = 2647, name = "Plate Legs", chance = 1242},
        [5] = {id = 2477, name = "Knight Legs", chance = 186},
         },
    [9813] = { -- Rare Rusty Legs
        [1] = {id = 2478, name = "Brass Legs", chance = 6500},
        [2] = {id = 2647, name = "Plate Legs", chance = 3800},
        [3] = {id = 2477, name = "Knight Legs", chance = 200},
        [4] = {id = 2488, name = "Crown Legs", chance = 52},
        [5] = {id = 2470, name = "Golden Legs", chance = 30}
        }
    }

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem = config[target.itemid]
    if not targetItem then
        return true
    end
-- rest of the script
When trying to use (in this case) a rust remover on the item, it always stops as if the targetItem always isn't the item in the table. Because I believe I'm getting the correct table value, ain't I? Am I just blind or am I missing something?

Currently learning more about tables, just wish to get some more clarification about the subject as it just doesn't want to go further than the check
Code:
 if not targetItem then return true
 
Last edited:
Ok so it isn't returning true, where is the rest of the code?
Code:
local x = 0
local config = {
    [9808] = { -- Common Rusty Armor
        [1] = {id = 2464, name = "Chain Armor", chance = 6994},
        [2] = {id = 2483, name = "Scale Armor", chance = 3952},
        [3] = {id = 2465, name = "Brass Armor", chance = 1502},
        [4] = {id = 2463, name = "Plate Armor", chance = 197}
        },                     
    [9809] = { -- Semi-rare Rusty Armor
        [1] = {id = 2483, name = "Scale Armor", chance = 6437},                 
        [2] = {id = 2464, name = "Chain Armor", chance = 4606},
        [3] = {id = 2465, name = "Brass Armor", chance = 3029},
        [4] = {id = 2463, name = "Plate Armor", chance = 1559},
        [5] = {id = 2476, name = "Knight Armor", chance = 595},
        [6] = {id = 8891, name = "Paladin Armor", chance = 283},
        [7] = {id = 2487, name = "Crown Armor", chance = 49}
        },               
    [9810] = { -- Rare Rusty Armor
        [1] = {id = 2465, name = "Brass Armor", chance = 6681},
        [2] = {id = 2463, name = "Plate Armor", chance = 3767},
        [3] = {id = 2476, name = "Knight Armor", chance = 1832},
        [4] = {id = 2487, name = "Crown Armor", chance = 177},
        [5] = {id = 8891, name = "Paladin Armor", chance = 31},
        [6] = {id = 2466, name = "Golden Armor", chance = 10}
        },                     
    [9811] = { -- Common Rusty Legs
        [1] = {id = 2648, name = "Chain Legs", chance = 6949},                 
        [2] = {id = 2468, name = "Studded Legs", chance = 3692},
        [3] = {id = 2478, name = "Brass Legs", chance = 1307},
        [4] = {id = 2647, name = "Plate Legs", chance = 133}
        },                 
    [9812] = { -- Semi-Rare Rusty Legs
        [1] = {id = 2468, name = "Studded Legs", chance = 5962},
        [2] = {id = 2648, name = "Chain Legs", chance = 4037},
        [3] = {id = 2478, name = "Brass Legs", chance = 2174},
        [4] = {id = 2647, name = "Plate Legs", chance = 1242},
        [5] = {id = 2477, name = "Knight Legs", chance = 186},
         },
    [9813] = { -- Rare Rusty Legs
        [1] = {id = 2478, name = "Brass Legs", chance = 6500},
        [2] = {id = 2647, name = "Plate Legs", chance = 3800},
        [3] = {id = 2477, name = "Knight Legs", chance = 200},
        [4] = {id = 2488, name = "Crown Legs", chance = 52},
        [5] = {id = 2470, name = "Golden Legs", chance = 30}
        }
    }

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem = config[target.itemid]
    if not targetItem then
        return true
    end

    local rand = math.random(10000)
    if config[targetItem] then
        for i = 1, #config[targetItem] do
            if rand <= config[targetItem][i].chance then
                x = i
            end
        end

        if x > 0 then
            if targetItem[i].id then
                target:transform(targetItem[i].id)
                toPosition:sendMagicEffect(CONST_ME_MAGIC_GREEN)
            else
                player:say((isInArray({9808, 9809, 9810}, target.itemid) and "The armor was already damaged so badly that it broke when you tried to clean it." or "The legs were already damaged so badly that they broke when you tried to clean them."),TALKTYPE_MONSTER_SAY)
                target:remove()
                toPosition:sendMagicEffect(CONST_ME_BLOCKHIT)
            end
            return item:remove(1)
        end
    end
end
Reason to why I didn't put it in the first post was that it isn't finished as I still ain't 100% sure about the tables
 
Code:
local config = {
    [9808] = { -- Common Rusty Armor
        [1] = {id = 2464, name = "Chain Armor", chance = 6994},
        [2] = {id = 2483, name = "Scale Armor", chance = 3952},
        [3] = {id = 2465, name = "Brass Armor", chance = 1502},
        [4] = {id = 2463, name = "Plate Armor", chance = 197}
        },                   
    [9809] = { -- Semi-rare Rusty Armor
        [1] = {id = 2483, name = "Scale Armor", chance = 6437},               
        [2] = {id = 2464, name = "Chain Armor", chance = 4606},
        [3] = {id = 2465, name = "Brass Armor", chance = 3029},
        [4] = {id = 2463, name = "Plate Armor", chance = 1559},
        [5] = {id = 2476, name = "Knight Armor", chance = 595},
        [6] = {id = 8891, name = "Paladin Armor", chance = 283},
        [7] = {id = 2487, name = "Crown Armor", chance = 49}
        },             
    [9810] = { -- Rare Rusty Armor
        [1] = {id = 2465, name = "Brass Armor", chance = 6681},
        [2] = {id = 2463, name = "Plate Armor", chance = 3767},
        [3] = {id = 2476, name = "Knight Armor", chance = 1832},
        [4] = {id = 2487, name = "Crown Armor", chance = 177},
        [5] = {id = 8891, name = "Paladin Armor", chance = 31},
        [6] = {id = 2466, name = "Golden Armor", chance = 10}
        },                   
    [9811] = { -- Common Rusty Legs
        [1] = {id = 2648, name = "Chain Legs", chance = 6949},               
        [2] = {id = 2468, name = "Studded Legs", chance = 3692},
        [3] = {id = 2478, name = "Brass Legs", chance = 1307},
        [4] = {id = 2647, name = "Plate Legs", chance = 133}
        },               
    [9812] = { -- Semi-Rare Rusty Legs
        [1] = {id = 2468, name = "Studded Legs", chance = 5962},
        [2] = {id = 2648, name = "Chain Legs", chance = 4037},
        [3] = {id = 2478, name = "Brass Legs", chance = 2174},
        [4] = {id = 2647, name = "Plate Legs", chance = 1242},
        [5] = {id = 2477, name = "Knight Legs", chance = 186},
         },
    [9813] = { -- Rare Rusty Legs
        [1] = {id = 2478, name = "Brass Legs", chance = 6500},
        [2] = {id = 2647, name = "Plate Legs", chance = 3800},
        [3] = {id = 2477, name = "Knight Legs", chance = 200},
        [4] = {id = 2488, name = "Crown Legs", chance = 52},
        [5] = {id = 2470, name = "Golden Legs", chance = 30}
        }
    }

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem, x = config[target.itemid], 0
    if not targetItem then
        return true
    end

    local rand = math.random(10000)
    -- targetItem is a table, config[targetItem] is nil
    for i = 1, #targetItem do
        if rand <= targetItem[i].chance then
            x = i
        end

        if x > 0 then
            if targetItem[i].id then
                target:transform(targetItem[i].id)
                toPosition:sendMagicEffect(CONST_ME_MAGIC_GREEN)
            else
                player:say((isInArray({9808, 9809, 9810}, target.itemid) and "The armor was already damaged so badly that it broke when you tried to clean it." or "The legs were already damaged so badly that they broke when you tried to clean them."),TALKTYPE_MONSTER_SAY)
                target:remove()
                toPosition:sendMagicEffect(CONST_ME_BLOCKHIT)
            end
            return item:remove(1)
        end
    end
    return true
end
 
Code:
local config = {
    [9808] = { -- Common Rusty Armor
        [1] = {id = 2464, name = "Chain Armor", chance = 6994},
        [2] = {id = 2483, name = "Scale Armor", chance = 3952},
        [3] = {id = 2465, name = "Brass Armor", chance = 1502},
        [4] = {id = 2463, name = "Plate Armor", chance = 197}
        },                  
    [9809] = { -- Semi-rare Rusty Armor
        [1] = {id = 2483, name = "Scale Armor", chance = 6437},              
        [2] = {id = 2464, name = "Chain Armor", chance = 4606},
        [3] = {id = 2465, name = "Brass Armor", chance = 3029},
        [4] = {id = 2463, name = "Plate Armor", chance = 1559},
        [5] = {id = 2476, name = "Knight Armor", chance = 595},
        [6] = {id = 8891, name = "Paladin Armor", chance = 283},
        [7] = {id = 2487, name = "Crown Armor", chance = 49}
        },            
    [9810] = { -- Rare Rusty Armor
        [1] = {id = 2465, name = "Brass Armor", chance = 6681},
        [2] = {id = 2463, name = "Plate Armor", chance = 3767},
        [3] = {id = 2476, name = "Knight Armor", chance = 1832},
        [4] = {id = 2487, name = "Crown Armor", chance = 177},
        [5] = {id = 8891, name = "Paladin Armor", chance = 31},
        [6] = {id = 2466, name = "Golden Armor", chance = 10}
        },                  
    [9811] = { -- Common Rusty Legs
        [1] = {id = 2648, name = "Chain Legs", chance = 6949},              
        [2] = {id = 2468, name = "Studded Legs", chance = 3692},
        [3] = {id = 2478, name = "Brass Legs", chance = 1307},
        [4] = {id = 2647, name = "Plate Legs", chance = 133}
        },              
    [9812] = { -- Semi-Rare Rusty Legs
        [1] = {id = 2468, name = "Studded Legs", chance = 5962},
        [2] = {id = 2648, name = "Chain Legs", chance = 4037},
        [3] = {id = 2478, name = "Brass Legs", chance = 2174},
        [4] = {id = 2647, name = "Plate Legs", chance = 1242},
        [5] = {id = 2477, name = "Knight Legs", chance = 186},
         },
    [9813] = { -- Rare Rusty Legs
        [1] = {id = 2478, name = "Brass Legs", chance = 6500},
        [2] = {id = 2647, name = "Plate Legs", chance = 3800},
        [3] = {id = 2477, name = "Knight Legs", chance = 200},
        [4] = {id = 2488, name = "Crown Legs", chance = 52},
        [5] = {id = 2470, name = "Golden Legs", chance = 30}
        }
    }

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem, x = config[target.itemid], 0
    if not targetItem then
        return true
    end

    local rand = math.random(10000)
    -- targetItem is a table, config[targetItem] is nil
    for i = 1, #targetItem do
        if rand <= targetItem[i].chance then
            x = i
        end

        if x > 0 then
            if targetItem[i].id then
                target:transform(targetItem[i].id)
                toPosition:sendMagicEffect(CONST_ME_MAGIC_GREEN)
            else
                player:say((isInArray({9808, 9809, 9810}, target.itemid) and "The armor was already damaged so badly that it broke when you tried to clean it." or "The legs were already damaged so badly that they broke when you tried to clean them."),TALKTYPE_MONSTER_SAY)
                target:remove()
                toPosition:sendMagicEffect(CONST_ME_BLOCKHIT)
            end
            return item:remove(1)
        end
    end
    return true
end
ty, just got one last issue. For some reason it ALWAYS picks the first indexed item in the table, for example: for item.id 9808 = chain armor, 9809 = scale armor etc...
 
ty, just got one last issue. For some reason it ALWAYS picks the first indexed item in the table, for example: for item.id 9808 = chain armor, 9809 = scale armor etc...
When in doubt print(rand) :), you will be using print a lot when writing scripts.
 
well if you loop through a table and start with the item of highest chance it'll ofcourse always pick the first and skip the rest.
either loop from lowest chance to highest chance or let it randomly pick indexes without looping over them until it finds one with the correct chance.
 
What I would do is use the size of the table and randomize that rather worry about a chance ratio.
Code:
local config = {
    [9808] = { -- Common Rusty Armor
        [1] = {id = 2464, name = "Chain Armor", chance = 6994},
        [2] = {id = 2483, name = "Scale Armor", chance = 3952},
        [3] = {id = 2465, name = "Brass Armor", chance = 1502},
        [4] = {id = 2463, name = "Plate Armor", chance = 197}
        [5] = {id = nil},
        [6] = {id = nil}
        },                
    }

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem, x = config[target.itemid], 0
    if not targetItem then
        return true
    end

    local rand = math.random(10000)

    local i = math.random(#targetItem)
    if targetItem[i].id then
        target:transform(targetItem[i].id)
        toPosition:sendMagicEffect(CONST_ME_MAGIC_GREEN)
    else
        player:say((isInArray({9808, 9809, 9810}, target.itemid) and "The armor was already damaged so badly that it broke when you tried to clean it." or "The legs were already damaged so badly that they broke when you tried to clean them."),TALKTYPE_MONSTER_SAY)
        target:remove()
        toPosition:sendMagicEffect(CONST_ME_BLOCKHIT)
    end
    return item:remove(1)
end
 
Last edited:
Ordering from lowest to higher is not a good option as well, because if you have an item with 90% of chance and after it an item with 95%, the chances of the 95% being picked is highly decreased by the one before it.

The best approach is to either randomize the order you index the table or you random them all and get the successfull results and them pick one randomly.
 
this should do the work
Code:
local config = {
  [9808] = { -- Common Rusty Armor
  {id = 2464, name = "Chain Armor", chance = 6994},
  {id = 2483, name = "Scale Armor", chance = 3952},
  {id = 2465, name = "Brass Armor", chance = 1502},
  {id = 2463, name = "Plate Armor", chance = 197}
  },  
  [9809] = { -- Semi-rare Rusty Armor
  {id = 2483, name = "Scale Armor", chance = 6437},  
  {id = 2464, name = "Chain Armor", chance = 4606},
  {id = 2465, name = "Brass Armor", chance = 3029},
  {id = 2463, name = "Plate Armor", chance = 1559},
  {id = 2476, name = "Knight Armor", chance = 595},
  {id = 8891, name = "Paladin Armor", chance = 283},
  {id = 2487, name = "Crown Armor", chance = 49}
  },  
  [9810] = { -- Rare Rusty Armor
  {id = 2465, name = "Brass Armor", chance = 6681},
  {id = 2463, name = "Plate Armor", chance = 3767},
  {id = 2476, name = "Knight Armor", chance = 1832},
  {id = 2487, name = "Crown Armor", chance = 177},
  {id = 8891, name = "Paladin Armor", chance = 31},
  {id = 2466, name = "Golden Armor", chance = 10}
  },  
  [9811] = { -- Common Rusty Legs
  {id = 2648, name = "Chain Legs", chance = 6949},  
  {id = 2468, name = "Studded Legs", chance = 3692},
  {id = 2478, name = "Brass Legs", chance = 1307},
  {id = 2647, name = "Plate Legs", chance = 133}
  },  
  [9812] = { -- Semi-Rare Rusty Legs
  {id = 2468, name = "Studded Legs", chance = 5962},
  {id = 2648, name = "Chain Legs", chance = 4037},
  {id = 2478, name = "Brass Legs", chance = 2174},
  {id = 2647, name = "Plate Legs", chance = 1242},
  {id = 2477, name = "Knight Legs", chance = 186},
  },
  [9813] = { -- Rare Rusty Legs
  {id = 2478, name = "Brass Legs", chance = 6500},
  {id = 2647, name = "Plate Legs", chance = 3800},
  {id = 2477, name = "Knight Legs", chance = 200},
  {id = 2488, name = "Crown Legs", chance = 52},
  {id = 2470, name = "Golden Legs", chance = 30}
  }
  }


function onUse(player, item, fromPosition, target, toPosition, isHotkey)
   local targetItem = config[target.itemid]
   if not targetItem then
  return true
  end
  
   local rand = math.random(10000)
   local index = false
  
   if targetItem[1].chance >= rand then -- implying first item in the table index always has the highest chance.
     while not index do
       randomIndex = math.random(#targetItem)
       if targetItem[randomIndex].chance >= rand then
         index = randomIndex
       end
     end
   end
  
   if not index then
     player:say((isInArray({9808, 9809, 9810}, target.itemid) and "The armor was already damaged so badly that it broke when you tried to clean it." or "The legs were already damaged so badly that they broke when you tried to clean them."),TALKTYPE_MONSTER_SAY)
  target:remove()
  toPosition:sendMagicEffect(CONST_ME_BLOCKHIT)
   else
     target:transform(targetItem[index].id)
  toPosition:sendMagicEffect(CONST_ME_MAGIC_GREEN)
   end
   return item:remove(1)
end
 
this should do the work
Code:
local config = {
  [9808] = { -- Common Rusty Armor
  {id = 2464, name = "Chain Armor", chance = 6994},
  {id = 2483, name = "Scale Armor", chance = 3952},
  {id = 2465, name = "Brass Armor", chance = 1502},
  {id = 2463, name = "Plate Armor", chance = 197}
  },
  [9809] = { -- Semi-rare Rusty Armor
  {id = 2483, name = "Scale Armor", chance = 6437},
  {id = 2464, name = "Chain Armor", chance = 4606},
  {id = 2465, name = "Brass Armor", chance = 3029},
  {id = 2463, name = "Plate Armor", chance = 1559},
  {id = 2476, name = "Knight Armor", chance = 595},
  {id = 8891, name = "Paladin Armor", chance = 283},
  {id = 2487, name = "Crown Armor", chance = 49}
  },
  [9810] = { -- Rare Rusty Armor
  {id = 2465, name = "Brass Armor", chance = 6681},
  {id = 2463, name = "Plate Armor", chance = 3767},
  {id = 2476, name = "Knight Armor", chance = 1832},
  {id = 2487, name = "Crown Armor", chance = 177},
  {id = 8891, name = "Paladin Armor", chance = 31},
  {id = 2466, name = "Golden Armor", chance = 10}
  },
  [9811] = { -- Common Rusty Legs
  {id = 2648, name = "Chain Legs", chance = 6949},
  {id = 2468, name = "Studded Legs", chance = 3692},
  {id = 2478, name = "Brass Legs", chance = 1307},
  {id = 2647, name = "Plate Legs", chance = 133}
  },
  [9812] = { -- Semi-Rare Rusty Legs
  {id = 2468, name = "Studded Legs", chance = 5962},
  {id = 2648, name = "Chain Legs", chance = 4037},
  {id = 2478, name = "Brass Legs", chance = 2174},
  {id = 2647, name = "Plate Legs", chance = 1242},
  {id = 2477, name = "Knight Legs", chance = 186},
  },
  [9813] = { -- Rare Rusty Legs
  {id = 2478, name = "Brass Legs", chance = 6500},
  {id = 2647, name = "Plate Legs", chance = 3800},
  {id = 2477, name = "Knight Legs", chance = 200},
  {id = 2488, name = "Crown Legs", chance = 52},
  {id = 2470, name = "Golden Legs", chance = 30}
  }
  }


function onUse(player, item, fromPosition, target, toPosition, isHotkey)
   local targetItem = config[target.itemid]
   if not targetItem then
  return true
  end

   local rand = math.random(10000)
   local index = false

   if targetItem[1].chance >= rand then -- implying first item in the table index always has the highest chance.
     while not index do
       randomIndex = math.random(#targetItem)
       if targetItem[randomIndex].chance >= rand then
         index = randomIndex
       end
     end
   end

   if not index then
     player:say((isInArray({9808, 9809, 9810}, target.itemid) and "The armor was already damaged so badly that it broke when you tried to clean it." or "The legs were already damaged so badly that they broke when you tried to clean them."),TALKTYPE_MONSTER_SAY)
  target:remove()
  toPosition:sendMagicEffect(CONST_ME_BLOCKHIT)
   else
     target:transform(targetItem[index].id)
  toPosition:sendMagicEffect(CONST_ME_MAGIC_GREEN)
   end
   return item:remove(1)
end
May cause infinite loop as you don't renew rand value.
Nvm
 
Code:
local config = {
    [9808] = { -- Common Rusty Armor
        [1] = {id = 2464, name = "Chain Armor", chance = 6994},
        [2] = {id = 2483, name = "Scale Armor", chance = 3952},
        [3] = {id = 2465, name = "Brass Armor", chance = 1502},
        [4] = {id = 2463, name = "Plate Armor", chance = 197}
    },
    [9809] = { -- Semi-rare Rusty Armor
        [1] = {id = 2483, name = "Scale Armor", chance = 6437},
        [2] = {id = 2464, name = "Chain Armor", chance = 4606},
        [3] = {id = 2465, name = "Brass Armor", chance = 3029},
        [4] = {id = 2463, name = "Plate Armor", chance = 1559},
        [5] = {id = 2476, name = "Knight Armor", chance = 595},
        [6] = {id = 8891, name = "Paladin Armor", chance = 283},
        [7] = {id = 2487, name = "Crown Armor", chance = 49}
    },
    [9810] = { -- Rare Rusty Armor
        [1] = {id = 2465, name = "Brass Armor", chance = 6681},
        [2] = {id = 2463, name = "Plate Armor", chance = 3767},
        [3] = {id = 2476, name = "Knight Armor", chance = 1832},
        [4] = {id = 2487, name = "Crown Armor", chance = 177},
        [5] = {id = 8891, name = "Paladin Armor", chance = 31},
        [6] = {id = 2466, name = "Golden Armor", chance = 10}
    },
    [9811] = { -- Common Rusty Legs
        [1] = {id = 2648, name = "Chain Legs", chance = 6949},
        [2] = {id = 2468, name = "Studded Legs", chance = 3692},
        [3] = {id = 2478, name = "Brass Legs", chance = 1307},
        [4] = {id = 2647, name = "Plate Legs", chance = 133}
    },
    [9812] = { -- Semi-Rare Rusty Legs
        [1] = {id = 2468, name = "Studded Legs", chance = 5962},
        [2] = {id = 2648, name = "Chain Legs", chance = 4037},
        [3] = {id = 2478, name = "Brass Legs", chance = 2174},
        [4] = {id = 2647, name = "Plate Legs", chance = 1242},
        [5] = {id = 2477, name = "Knight Legs", chance = 186},
    },
    [9813] = { -- Rare Rusty Legs
        [1] = {id = 2478, name = "Brass Legs", chance = 6500},
        [2] = {id = 2647, name = "Plate Legs", chance = 3800},
        [3] = {id = 2477, name = "Knight Legs", chance = 200},
        [4] = {id = 2488, name = "Crown Legs", chance = 52},
        [5] = {id = 2470, name = "Golden Legs", chance = 30}
    }
}

for id, settings in pairs(config) do
    local chance = 1
    for i = 1, #settings do
        local setting = settings[i]
        setting.chance = {chance, chance + setting.chance}
        chance = setting.chance[2] + 1
    end
    settings.total = chance - 1
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem = config[target.itemid]
    if not targetItem then
        return true
    end

    local rand = math.random(1, targetItem.total)
    for i = 1, #targetItem do
        local chance = targetItem[i].chance
        if rand >= chance[1] and rand <= chance[2] then
            if targetItem[i].id then
                target:transform(targetItem[i].id)
                toPosition:sendMagicEffect(CONST_ME_MAGIC_GREEN)
            else
                target:remove()
                player:say((isInArray({9808, 9809, 9810}, target.itemid) and "The armor was already damaged so badly that it broke when you tried to clean it." or "The legs were already damaged so badly that they broke when you tried to clean them."), TALKTYPE_MONSTER_SAY)
                toPosition:sendMagicEffect(CONST_ME_BLOCKHIT)
            end
            item:remove(1)
            return true
        end
    end
    return true
end
 
Back
Top