• 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 [Solved] Same item with different actionid

exanime

I´m learning Lua
Joined
Oct 24, 2011
Messages
40
Reaction score
3
Location
Bogota, Colombia
Hello, I have come to ask for help, I want the same item that I get from a quest, have different actionid, some help?

It will be an item with different actionid, being the Legendary item with less possibility of being given to the player, also that the description be added

I have this script

TFS 1.3


I still do not handle the metatables very well, any explanation :p ?

Code:
local ids = {
    {thing = "Normal", chance = 85, itemID = 2475, actionID = 45113}, --85%
    {thing = "Bound", chance = 13, itemID = 2475, actionID = 45114}, --13%
    {thing = "Legendary", chance = 2, itemID = 2475, actionID = 45115} --2%
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
     local chance = math.random(1, 100)
    if chance < ids.chance then
    local Legs = player:additem(ids[chance].itemID,1,true, CONST_SLOT_WHEREEVER)
     item:setActionId(Legs, ids[chance].actionID)
     item:setAttribute(Legs, "description", ids[chance].thing)
      player:sendTextMessage(TALKTYPE_ORANGE_1, "Random Legs Acquired!")
     end
     return true
end
 
Solution
try
Code:
local weight = ItemType(ids.itemID):getWeight()


Edit: No it's not gonna work. You're doing this wrong. Hold on.

I have no idea why you compare weight to storage value.


Try this:
Code:
local storage = 10405
local ids = {
    {thing = "Legendary", chance = 2, itemID = 2475, actionID = 45115}, --2%
    {thing = "Bound", chance = 13, itemID = 2475, actionID = 45114}, --13%
    {thing = "Normal", chance = 85, itemID = 2475, actionID = 45113} --85%
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if player:getStorageValue(storage) ~= -1 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local rand = math.random(1, 100)
    for _, chanceItem in...
Took me awhile to figure out the issue. xD
You want to loop through your table, checking each chance as you go along.
You'll want to have the 'lower chance' nearer to the top of your table.

Code:
local ids = {
   [1] = {thing = "Legendary", chance = 2, itemID = 2475, actionID = 45115}, -- 2% "1 or 2"
   [2] = {thing = "Bound", chance = 15, itemID = 2475, actionID = 45114},    -- 13% "3,4,...,14,15"
   [3] = {thing = "Normal", chance = 100, itemID = 2475, actionID = 45113}   -- 85% "16,17,...,99,100"
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
   local rand = math.random(1, 100)
   for i = 1, #ids do
       if rand <= ids.chance then
           local Legs = player:additem(ids[i].itemID, 1, true, CONST_SLOT_WHEREEVER)
           item:setActionId(Legs, ids[i].actionID)
           item:setAttribute(Legs, "description", ids[i].thing)
           player:sendTextMessage(TALKTYPE_ORANGE_1, "Random Legs Acquired!")
       end
       break
   end
   return true
end
 
Code:
local ids = {
   [1] = {thing = "Legendary", chance = 2, itemID = 2475, actionID = 45115}, -- 2% "1 or 2"
   [2] = {thing = "Bound", chance = 15, itemID = 2475, actionID = 45114},    -- 13% "3,4,...,14,15"
   [3] = {thing = "Normal", chance = 100, itemID = 2475, actionID = 45113}   -- 85% "16,17,...,99,100"
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local rand = math.random(1, 100)
    for i = 1, #ids do
        if rand <= ids.chance then
            local legs = player:additem(ids[i].itemID)
            legs:setActionId(ids[i].actionID)
            legs:setAttribute("description", ids[i].thing)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "Random Legs Acquired!")
            break
        end
    end
    return true
end
 
Code:
local ids = {
   [1] = {thing = "Legendary", chance = 2, itemID = 2475, actionID = 45115}, -- 2% "1 or 2"
   [2] = {thing = "Bound", chance = 15, itemID = 2475, actionID = 45114},    -- 13% "3,4,...,14,15"
   [3] = {thing = "Normal", chance = 100, itemID = 2475, actionID = 45113}   -- 85% "16,17,...,99,100"
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
   local rand = math.random(1, 100)
   for i = 1, #ids do
      if rand <= ids.chance then
          local legs = player:additem(ids[i].itemID)
          legs:setActionId(ids[i].actionID)
          legs:setAttribute("description", ids[i].thing)
          player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "Random Legs Acquired!")
      end
      break
   end
   return true
end
Thanks. lol
I knew the main issue, I had no idea if the rest of his script was setup to work correctly or not. :rolleyes:
 
Sorry...
i get this error

Lua Script Error: [Action Interface]
data/actions.../name.lua:attemp to compare number with nil
Stack traceback_
[C]: in function '__le'
data/actions.../name.lua:10: in function <data/actions../name.lua:7>
 
I edited the code a bit but it throws me an error, could they help me?
Code:
local storage = 10405
local ids = {
    {thing = "Normal", chance = 85, itemID = 2475, actionID = 45113}, --85%
    {thing = "Bound", chance = 13, itemID = 2475, actionID = 45114}, --13%
    {thing = "Legendary", chance = 2, itemID = 2475, actionID = 45115} --2%
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
local Player_storage = player:getStorageValue(storage)
local weight = itemType:getWeight(ids.itemID)
    
if player:getStorageValue(storage) == -1 then
    if Player_storage >= weight then
    local rand = math.random(1, 100)
    for i = 1, #ids do
        if rand <= ids.chance then
            local legs = player:additem(ids[i].itemID)
            legs:setActionId(ids[i].actionID)
            legs:setAttribute("description", ids[i].thing)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "Random Legs Acquired!")
            player:setStorageValue(storage, 1)
            break
        end
          
    end
    elseif Player_storage < weight then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It too heavy")
end
elseif player:getStorageValue(storage) >= 1 then
    player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
end
return true
end
I got this error

d4141c2aeab5202d54080023c2522f64o.png



I would appreciate it a lot
 
Last edited:
try
Code:
local weight = ItemType(ids.itemID):getWeight()


Edit: No it's not gonna work. You're doing this wrong. Hold on.

I have no idea why you compare weight to storage value.


Try this:
Code:
local storage = 10405
local ids = {
    {thing = "Legendary", chance = 2, itemID = 2475, actionID = 45115}, --2%
    {thing = "Bound", chance = 13, itemID = 2475, actionID = 45114}, --13%
    {thing = "Normal", chance = 85, itemID = 2475, actionID = 45113} --85%
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if player:getStorageValue(storage) ~= -1 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local rand = math.random(1, 100)
    for _, chanceItem in ipairs(ids) do
        if rand <= chanceItem.chance then
            local weight = ItemType(chanceItem.itemID):getWeight()
            if player:getCapacity() < weight then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "It's too heavy.")
                return true
            end

            local legs = player:addItem(chanceItem.itemID)
            legs:setActionId(chanceItem.actionID)
            legs:setAttribute("description", chanceItem.thing)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "Random legs obtained!")
            player:setStorageValue(storage, 1)
            return true
        end     
    end
    return true
end

Note: more rare items must be on top of table. Don't modify the order.
 
Last edited:
Solution
try
Code:
local weight = ItemType(ids.itemID):getWeight()


Edit: No it's not gonna work. You're doing this wrong. Hold on.

I have no idea why you compare weight to storage value.


Try this:
Code:
local storage = 10405
local ids = {
    {thing = "Legendary", chance = 2, itemID = 2475, actionID = 45115}, --2%
    {thing = "Bound", chance = 13, itemID = 2475, actionID = 45114}, --13%
    {thing = "Normal", chance = 85, itemID = 2475, actionID = 45113} --85%
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if player:getStorageValue(storage) ~= -1 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local rand = math.random(1, 100)
    for _, chanceItem in ipairs(ids) do
        if rand <= chanceItem.chance then
            local weight = ItemType(chanceItem.itemID):getWeight()
            if player:getCapacity() < weight then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "It's too heavy.")
                return true
            end

            local legs = player:additem(chanceItem.itemID)
            legs:setActionId(chanceItem.actionID)
            legs:setAttribute("description", chanceItem.thing)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "Random legs obtained!")
            player:setStorageValue(storage, 1)
            return true
        end     
    end
    return true
end

Note: more rare items must be on top of table. Don't modify the order.
The percents are all fudged up again as well.
2-15-100 = 2%-13%-85%
 
I get an error, I also do not understand where the variable chanceItem


And I do not understand about the player : additem (... ?????) function...


I understand that the main one player:addItem(2400, 1, true, 1, CONST_SLOT_WHEREEVER) function.

10f403cdfa004d0208b76c67e706a266o.png
 
Back
Top