• 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!
  • If you're using Gesior 2012 or MyAAC, please review this thread for information about a serious security vulnerability and a fix.

Lua Help simplifying the script

MorganaSacani

Active Member
Joined
Sep 20, 2022
Messages
78
Solutions
1
Reaction score
28
I'm creating a script, but I will have to do a lot of repetitions. For that I need to understand how to create a loop and make my work easier
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    -- Trees

    -- Stones
    
    -- Corpses
    if itemEx.itemid == 2813 then -- Dead Rat
        if math.random(1, 100) <= 60 then
            doPlayerAddItem(cid, 2666, 3) -- Raw Meat
        else
            doPlayerAddItem(cid, 5878, 1) -- Leather
        end
        if math.random(1, 100) <= 30 then
            doTransformItem(itemEx.uid, 2815)
            -- doDecayItem(itemEx.uid)
        end
    end
    

    return true
end

I thought of something like this, but I don't know how to do it:
Lua:
local TARGETS = {
    [2813] = {
        [2666] = 3,    -- Raw meat
        [5878] = 1, -- Leather
    }
}

I need to add the doTransformItem in the loop too but I don't know how
 
Solution
This problem happens when I try to use the pick on some other itemId that is not in the table.

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if not config[itemEx.itemid] then
        -- in case you want to notify the player
        -- doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, "You cannot use foo on bar.")
        return true
    end

    local addItemTable = config[itemEx.itemid].addItem
    -- etc etc
end

amatria

Well-Known Member
Joined
May 15, 2021
Messages
74
Solutions
6
Reaction score
78
Location
Spain
GitHub
amatria
I hope this helps:

Lua:
local config = {
  [2813] = {
    addItem = {
      chance = 60,
      success = {id = 2666, qty = 3},
      fail = {id = 5878, qty = 1}
    },
    doTransform = {
      chance = 30,
      success = {id = 2815}
    }
  }
}

-- function onUse(...)
local addItemTable = config[itemEx.itemid].addItem
local doTransformTable = config[itemEx.itemid].doTransform

if math.random(1, 100) <= addItemTable.chance then
  -- doPlayerAddItem(cid, addItemTable.success.id, addItemTable.success.qty)
else
  -- doPlayerAddItem(cid, addItemTable.fail.id, addItemTable.fail.qty)
end

if math.random(1, 100) <= doTransformTable.chance then
  -- doTransformItem(itemEx.uid, doTransformTable.success.id)
else
  -- doDecayItem(itemEx.uid)
end
 
Last edited:
OP
OP
M

MorganaSacani

Active Member
Joined
Sep 20, 2022
Messages
78
Solutions
1
Reaction score
28
[19:30:29] [Error - Action Interface]
[19:30:29] data/actions/scripts/tools/pick.lua:eek:nUse
[19:30:29] Description:
[19:30:29] data/actions/scripts/tools/pick.lua:16: attempt to index a nil value
[19:30:29] stack traceback:
[19:30:29] data/actions/scripts/tools/pick.lua:16: in function <data/actions/scripts/tools/pick.lua:15>

Lua:
local config = {
  [2813] = {
      addItem = {
          chance = 75,
          success = {id = 2666, qty = 3},
          fail = {id = 5878, qty = 1}
      },
      doTransform = {
          chance = 20,
          success = {id = 2814}
      }
  }
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local addItemTable = config[itemEx.itemid].addItem
    local doTransformTable = config[itemEx.itemid].doTransform

    local stamina = 5
    if getPlayerAttrStamina(cid) >= stamina then
        doPlayerRemoveAttrStamina(cid, stamina)
        if math.random(1, 100) <= addItemTable.chance then
            doPlayerAddItem(cid, addItemTable.success.id, addItemTable.success.qty)
            doSendMagicEffect(toPosition, CONST_ME_BLOCKHIT)
        else
            doPlayerAddItem(cid, addItemTable.fail.id, addItemTable.fail.qty)
            doSendMagicEffect(toPosition, CONST_ME_BLOCKHIT)
        end

        if math.random(1, 100) <= doTransformTable.chance then
            doDecayItem(itemEx.uid)
            doTransformItem(itemEx.uid, doTransformTable.success.id)
            doSendMagicEffect(toPosition, CONST_ME_POFF)
        end
    else
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, "You are out of vigor.")
    end

    if math.random(1, 100) <= 10 then doPlayerRemoveAttrFood(cid, 1) end
    -- if math.random(1, 100) <= 20 then doPlayerRemoveAttrWater(cid, 1) end

    return true
end

This problem happens when I try to use the pick on some other itemId that is not in the table.
 
Last edited:

amatria

Well-Known Member
Joined
May 15, 2021
Messages
74
Solutions
6
Reaction score
78
Location
Spain
GitHub
amatria
This problem happens when I try to use the pick on some other itemId that is not in the table.

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if not config[itemEx.itemid] then
        -- in case you want to notify the player
        -- doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, "You cannot use foo on bar.")
        return true
    end

    local addItemTable = config[itemEx.itemid].addItem
    -- etc etc
end
 
Solution
OP
OP
M

MorganaSacani

Active Member
Joined
Sep 20, 2022
Messages
78
Solutions
1
Reaction score
28
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if not config[itemEx.itemid] then
        -- in case you want to notify the player
        -- doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, "You cannot use foo on bar.")
        return true
    end

    local addItemTable = config[itemEx.itemid].addItem
    -- etc etc
end
Wow thx!
 
Top