• 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 Removing the item after use

kite28

Member
Joined
May 15, 2012
Messages
69
Reaction score
5
I would like the console to not throw an error after using the item, can anyone help me?

Code:
local items = {
{itemId = 13432, count = 1, chance = 10}, -- ITEM, QUANTIDADE, CHANCE
{itemId = 13433, count = 1, chance = 10},
{itemId = 13434, count = 1, chance = 10},
{itemId = 13435, count = 1, chance = 10},
{itemId = 13436, count = 1, chance = 10},
{itemId = 13437, count = 1, chance = 10},
{itemId = 13444, count = 1, chance = 10},
}
local broadcastitems = {13432, 13433, 13434, 13435, 13436, 13437, 13444}

function onUse(cid, item, frompos, item2, topos)

  local config = {
    pos = getCreaturePosition(cid), -- posição do player
    exhaustionSeconds = 3600, -- exausted em segundos
    storageUse = 44231, -- storage usado.

  }

  if(exhaustion.check(cid, config.storageUse)) then
    if (exhaustion.get(cid, config.storageUse) >= 60) then
      doPlayerSendCancel(cid, "You can only open the chest again in " .. math.floor(exhaustion.get(cid, config.storageUse) / 60 + 1) .." minutes.")
    end
    if (exhaustion.get(cid, config.storageUse) <= 60) then
      doPlayerSendCancel(cid, "You need to wait " .. exhaustion.get(cid, config.storageUse).." seconds.")
    end
    return true
  end

  local totalChance, randomTable, randomNumber = 0, {}, 0

  addEvent(doPlayerSendTextMessage, 1*1000, cid, 27, "Wait...")
  addEvent(doPlayerSendTextMessage, 2*1000, cid, 27, "Wait...")
  addEvent(doPlayerSendTextMessage, 3*1000, cid, 27, "Wait...")
  addEvent(doPlayerSendTextMessage, 4*1000, cid, 27, "Wait...")
  addEvent(doPlayerSendTextMessage, 5*1000, cid, 27, "Wait...")
  addEvent(doSendAnimatedText, 1*1000, config.pos, "[5]", 180)
  addEvent(doSendAnimatedText, 2*1000, config.pos, "[4]", 180)
  addEvent(doSendAnimatedText, 3*1000, config.pos, "[3]", 180)
  addEvent(doSendAnimatedText, 4*1000, config.pos, "[2]", 180)
  addEvent(doSendAnimatedText, 5*1000, config.pos, "[1]", 180)
  addEvent(doSendAnimatedText, 6*1000, config.pos, "DONE!", 180)

  for _, itemInfo in pairs (items) do
    randomTable[itemInfo.itemId] = {min = totalChance + 1, max = itemInfo.chance, count = itemInfo.count or 1}
    totalChance = totalChance + itemInfo.chance
  end

  randomNumber = math.random(1, totalChance)
  for itemId, itemInfo in pairs (randomTable) do
    local min, max = itemInfo.min, itemInfo.min + itemInfo.max
    if randomNumber >= min and randomNumber <= max then
      local newItem = addEvent(doPlayerAddItem, 6*1000, cid, itemId, itemInfo.count, false)
      if not newItem then return doPlayerSendCancel(cid, "You do not have space to receive the item!") end
      doRemoveItem(item.uid, 0)
      exhaustion.set(cid, config.storageUse, config.exhaustionSeconds)
      local iInfo = getItemInfo(itemId)
      addEvent(doPlayerSendTextMessage, 6*1000, cid, 27, "You found ("..iInfo.name..")!")
      for i, broaditem in ipairs(broadcastitems) do
        if itemId==broaditem then addEvent(doBroadcastMessage, 6*1000, getCreatureName(cid).." just won: "..iInfo.name..".") end
        doRemoveItem(13452, 1)
      end
      break
    end
  end
  return true
end



Lua:
[11:28:26.731] [Error - Action Interface]
[11:28:26.731] data/actions/scripts/rare.lua:onUse
[11:28:26.732] Description:
[11:28:26.732] (LuaInterface::luaDoRemoveItem) Item not found
 
Lua:
doRemoveItem(13452, 1)
This line is attempting to remove an item, but you have no check for whether or not the player has the item.
Lua:
local items = {
    {itemId = 13432, count = 1, chance = 10}, -- ITEM, QUANTIDADE, CHANCE
    {itemId = 13433, count = 1, chance = 10},
    {itemId = 13434, count = 1, chance = 10},
    {itemId = 13435, count = 1, chance = 10},
    {itemId = 13436, count = 1, chance = 10},
    {itemId = 13437, count = 1, chance = 10},
    {itemId = 13444, count = 1, chance = 10}
}
local broadcastitems = {13432, 13433, 13434, 13435, 13436, 13437, 13444}

function onUse(cid, item, frompos, item2, topos)
    local config = {
        pos = getCreaturePosition(cid), -- posição do player
        exhaustionSeconds = 3600, -- exausted em segundos
        storageUse = 44231 -- storage usado.
    }

    if (exhaustion.check(cid, config.storageUse)) then
        if (exhaustion.get(cid, config.storageUse) >= 60) then
            doPlayerSendCancel(
                cid,
                "You can only open the chest again in " ..
                    math.floor(exhaustion.get(cid, config.storageUse) / 60 + 1) .. " minutes."
            )
        end
        if (exhaustion.get(cid, config.storageUse) <= 60) then
            doPlayerSendCancel(cid, "You need to wait " .. exhaustion.get(cid, config.storageUse) .. " seconds.")
        end
        return true
    end

    local totalChance, randomTable, randomNumber = 0, {}, 0

    addEvent(doPlayerSendTextMessage, 1 * 1000, cid, 27, "Wait...")
    addEvent(doPlayerSendTextMessage, 2 * 1000, cid, 27, "Wait...")
    addEvent(doPlayerSendTextMessage, 3 * 1000, cid, 27, "Wait...")
    addEvent(doPlayerSendTextMessage, 4 * 1000, cid, 27, "Wait...")
    addEvent(doPlayerSendTextMessage, 5 * 1000, cid, 27, "Wait...")
    addEvent(doSendAnimatedText, 1 * 1000, config.pos, "[5]", 180)
    addEvent(doSendAnimatedText, 2 * 1000, config.pos, "[4]", 180)
    addEvent(doSendAnimatedText, 3 * 1000, config.pos, "[3]", 180)
    addEvent(doSendAnimatedText, 4 * 1000, config.pos, "[2]", 180)
    addEvent(doSendAnimatedText, 5 * 1000, config.pos, "[1]", 180)
    addEvent(doSendAnimatedText, 6 * 1000, config.pos, "DONE!", 180)

    for _, itemInfo in pairs(items) do
        randomTable[itemInfo.itemId] = {min = totalChance + 1, max = itemInfo.chance, count = itemInfo.count or 1}
        totalChance = totalChance + itemInfo.chance
    end

    randomNumber = math.random(1, totalChance)
    for itemId, itemInfo in pairs(randomTable) do
        local min, max = itemInfo.min, itemInfo.min + itemInfo.max
        if randomNumber >= min and randomNumber <= max then
            local newItem = addEvent(doPlayerAddItem, 6 * 1000, cid, itemId, itemInfo.count, false)
            if not newItem then
                return doPlayerSendCancel(cid, "You do not have space to receive the item!")
            end
            doRemoveItem(item.uid, 0)
            exhaustion.set(cid, config.storageUse, config.exhaustionSeconds)
            local iInfo = getItemInfo(itemId)
            addEvent(doPlayerSendTextMessage, 6 * 1000, cid, 27, "You found (" .. iInfo.name .. ")!")
            for i, broaditem in ipairs(broadcastitems) do
                if itemId == broaditem then
                    addEvent(doBroadcastMessage, 6 * 1000, getCreatureName(cid) .. " just won: " .. iInfo.name .. ".")
                end
                doRemoveItem(13452, 1)
            end
            break
        end
    end
    return true
end
 
It seems to me that the problem lies somewhere else, because the item is removed when clicked, and the error throws 5x, which is as much as there is a countdown
 
It seems to me that the problem lies somewhere else, because the item is removed when clicked, and the error throws 5x, which is as much as there is a countdown
Lua:
            for i, broaditem in ipairs(broadcastitems) do
                if itemId == broaditem then
                    addEvent(doBroadcastMessage, 6 * 1000, getCreatureName(cid) .. " just won: " .. iInfo.name .. ".")
                end
                doRemoveItem(13452, 1)
            end

You are looping through broadcastitems and removing the item on each iteration.
Lua:
local broadcastitems = {13432, 13433, 13434, 13435, 13436, 13437, 13444}
broadcastitems as you can see, has 7 items.
So you'd be attempting to remove 12452 7 times.

If your character only has 1 of these items, it'd error 6x.
 
Lua:
local items = {
    {itemId = 13432, count = 1, chance = 10},
    {itemId = 13433, count = 1, chance = 10},
    {itemId = 13434, count = 1, chance = 10},
    {itemId = 13435, count = 1, chance = 10},
    {itemId = 13436, count = 1, chance = 10},
    {itemId = 13437, count = 1, chance = 10},
    {itemId = 13444, count = 1, chance = 10}
}
local broadcastitems = {13432, 13433, 13434, 13435, 13436, 13437, 13444}

function onUse(cid, item, frompos, item2, topos)
    local config = {
        pos = getCreaturePosition(cid),
        exhaustionSeconds = 3600,
        storageUse = 44231
    }

    if (exhaustion.check(cid, config.storageUse)) then
        local remainingTime = exhaustion.get(cid, config.storageUse)
        if remainingTime >= 60 then
            doPlayerSendCancel(
                cid,
                "You can only open the chest again in " ..
                    math.floor(remainingTime / 60 + 1) .. " minutes."
            )
        else
            doPlayerSendCancel(cid, "You need to wait " .. remainingTime .. " seconds.")
        end
        return true
    end

    local totalChance, randomTable, randomNumber = 0, {}, 0

    for _, itemInfo in pairs(items) do
        randomTable[itemInfo.itemId] = {min = totalChance + 1, max = totalChance + itemInfo.chance, count = itemInfo.count or 1}
        totalChance = totalChance + itemInfo.chance
    end

    randomNumber = math.random(1, totalChance)
    local selectedItemId = nil

    for itemId, itemInfo in pairs(randomTable) do
        local min, max = itemInfo.min, itemInfo.max
        if randomNumber >= min and randomNumber <= max then
            selectedItemId = itemId
            break
        end
    end

    if selectedItemId then
        local itemInfo = getItemInfo(selectedItemId)
        doPlayerAddItem(cid, selectedItemId, items[selectedItemId].count, false)
        doRemoveItem(item.uid, 1)
        exhaustion.set(cid, config.storageUse, config.exhaustionSeconds)
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You found (" .. itemInfo.name .. ")!")

        if table.contains(broadcastitems, selectedItemId) then
            doBroadcastMessage(getCreatureName(cid) .. " just won: " .. itemInfo.name .. ".")
        end
    else
        doPlayerSendCancel(cid, "Sorry, you didn't win anything this time.")
    end

    return true
end

function table.contains(table, value)
    for _, v in ipairs(table) do
        if v == value then
            return true
        end
    end
    return false
end


local countdownMessages = {"[5]", "[4]", "[3]", "[2]", "[1]", "DONE!"}
for i, message in ipairs(countdownMessages) do
    addEvent(doPlayerSendTextMessage, i * 1000, cid, 27, "Wait...")
    addEvent(doSendAnimatedText, i * 1000, config.pos, message, 180)
end
not tested
 
Last edited:

Similar threads

Back
Top