• 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.X+ On use 1 of 5 item get a new item

Eduardo170

Well-Known Member
Joined
Jan 7, 2014
Messages
422
Solutions
3
Reaction score
66
Location
Caracas, Venezuela
I try this
Example: U have 5 items on your backpack, you use 1 of 5 and remove it then get a new item
Code:
local hotaItems =  {2335, 2336, 2337, 2338, 2339, 2340, 2341}-- Items to check if you have and remove when using any of them.
local HotaComplete = 2343 -- new item

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local Hota = hotaItems[item.itemid]
    for hotaItems = 2335, 2341 do
            if player:getItemCount(id) < 1 then
                return false
            end
        end
    for hotaItems = 13540, 13545 do
            player:removeItem(hotaItems, 1)
        end
        player:addItem(HotaComplete, 1)
        player:getPosition():sendMagicEffect(CONST_ME_STUN)
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received % a new piece on your backpack.')
  
    end
    return true
    end
end
 
Solution
Lua:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaComplete = 2343

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if not isInArray(hotaItems, item.itemid) then
        print('not in array' .. item.itemid)
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i], true)
        if not hotaItem then
            print("player is missing item " .. hotaItems[i])
            return true
        end

        items[#items + 1] = hotaItem
        print('items[#items + 1] = hotaItem')
    end


    for i = 1, #items do
        print('items[i]:remove()')
        items[i]:remove()
    end...
XML:
<action fromid="2335" toid="2341" script="transform.lua" />

Lua:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaComplete = 2343

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not isInArray(hotaItems, item.itemid) then
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i])
        if not hotaItem then
            return true
        end

        items[#items + 1] = hotaItem
    end

    for i = 1, #items do
        items[i]:remove()
    end

    player:addItem(HotaComplete, 1)
    player:getPosition():sendMagicEffect(CONST_ME_STUN)
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received % a new piece on your backpack.')

    return true
end
Not tested, but should work as you wanted.
 
Last edited:
XML:
<action fromid="2335" toid="2341" script="transform.lua" />
Lua:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaComplete = 2343

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not isInArray(hotaItems, item.itemid) then
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i])
        if not hotaItem then
            return true
        end

        items[#items + 1] = hotaItem
    end

    for i = 1, #items do
        items[i]:remove()
    end

    player:addItem(HotaComplete, 1)
    player:getPosition():sendMagicEffect(CONST_ME_STUN)
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received % a new piece on your backpack.')

    return true
end
Not tested, but should work as you wanted.
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/hota.lua:onUse
data/actions/scripts/hota.lua:11: attempt to get length of global 'helmetIds' (a nil value)
stack traceback:
        [C]: in function '__len'
        data/actions/scripts/hota.lua:11: in function <data/actions/scripts/hota.lua:4>
 
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/hota.lua:onUse
data/actions/scripts/hota.lua:11: attempt to get length of global 'helmetIds' (a nil value)
stack traceback:
        [C]: in function '__len'
        data/actions/scripts/hota.lua:11: in function <data/actions/scripts/hota.lua:4>
Edited post, try now
 
Not work
You set a new variable?
Code:
 local items = {}
    local hotaItem
Lua:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaComplete = 2343

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not isInArray(hotaItems, item.itemid) then
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i])
        if not hotaItem then
            return true
        end

        items[#items + 1] = hotaItem
    end

    for i = 1, #items do
        items[i]:remove()
    end

    player:addItem(HotaComplete, 1)
    player:getPosition():sendMagicEffect(CONST_ME_STUN)
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received % a new piece on your backpack.')

    return true
end
This should work, I'm going to sleep now
 
Lua:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaComplete = 2343

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not isInArray(hotaItems, item.itemid) then
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i])
        if not hotaItem then
            return true
        end

        items[#items + 1] = hotaItem
    end

    for i = 1, #items do
        items[i]:remove()
    end

    player:addItem(HotaComplete, 1)
    player:getPosition():sendMagicEffect(CONST_ME_STUN)
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received % a new piece on your backpack.')

    return true
end
This should work, I'm going to sleep now
It doesn't work. Thank you
 
Okay, so do you need to have all 5 items to get the new item? or just 1 item you gives you the new item?
 
Could you add a print after line 4, 18 and 22 and post the console output

Code:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaComplete = 2343

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    print('if not isInArray(hotaItems, item.itemid) then')
    if not isInArray(hotaItems, item.itemid) then
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i])
        if not hotaItem then
            return true
        end
        
        items[#items + 1] = hotaItem
    end
    print('for i = 1, #items do')

    for i = 1, #items do
        print('items[i]:remove()')
        items[i]:remove()
    end

    player:addItem(HotaComplete, 1)
    player:getPosition():sendMagicEffect(CONST_ME_STUN)
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received % a new piece on your backpack.')

    return true
end
My console printed this.
Code:
if not isInArray(hotaItems, item.itemid) then
 
Could you print item.itemid after the first print
No print nothing...
Code:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaComplete = 2343

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if not isInArray(hotaItems, item.itemid) then
        print('item.itemid')
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i])
        if not hotaItem then
            return true
        end
 
        items[#items + 1] = hotaItem
        print('items[#items + 1] = hotaItem')
    end


    for i = 1, #items do
        print('items[i]:remove()')
        items[i]:remove()
    end

    player:addItem(HotaComplete, 1)
    player:getPosition():sendMagicEffect(CONST_ME_STUN)
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received % a new piece on your backpack.')

    return true
end
 
Last edited:
Lua:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaComplete = 2343

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if not isInArray(hotaItems, item.itemid) then
        print('not in array' .. item.itemid)
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i])
        if not hotaItem then
            print("player is missing item " .. hotaItems[i])
            return true
        end

        items[#items + 1] = hotaItem
        print('items[#items + 1] = hotaItem')
    end


    for i = 1, #items do
        print('items[i]:remove()')
        items[i]:remove()
    end

    player:addItem(HotaComplete, 1)
    player:getPosition():sendMagicEffect(CONST_ME_STUN)
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received % a new piece on your backpack.')

    return true
end
what does this print
 
Lua:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaComplete = 2343

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if not isInArray(hotaItems, item.itemid) then
        print('not in array' .. item.itemid)
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i])
        if not hotaItem then
            print("player is missing item " .. hotaItems[i])
            return true
        end

        items[#items + 1] = hotaItem
        print('items[#items + 1] = hotaItem')
    end


    for i = 1, #items do
        print('items[i]:remove()')
        items[i]:remove()
    end

    player:addItem(HotaComplete, 1)
    player:getPosition():sendMagicEffect(CONST_ME_STUN)
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received % a new piece on your backpack.')

    return true
end
what does this print
Code:
player is missing item 2335
player is missing item 2335
player is missing item 2335
 
Lua:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaComplete = 2343

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if not isInArray(hotaItems, item.itemid) then
        print('not in array' .. item.itemid)
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i], true)
        if not hotaItem then
            print("player is missing item " .. hotaItems[i])
            return true
        end

        items[#items + 1] = hotaItem
        print('items[#items + 1] = hotaItem')
    end


    for i = 1, #items do
        print('items[i]:remove()')
        items[i]:remove()
    end

    player:addItem(HotaComplete, 1)
    player:getPosition():sendMagicEffect(CONST_ME_STUN)
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received % a new piece on your backpack.')

    return true
end
Try that and make sure the player has that item
 
Solution
Thank you, I added a storage for use one only time.
Code:
local hotaItems = {2335, 2336, 2337, 2338, 2339, 2340, 2341}
local HotaCompleted = 2343


function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local itemType = ItemType(HotaCompleted)
    if player:getStorageValue(Storage.Hota) == 1 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You had this quest ')
        return true
    end
    if not isInArray(hotaItems, item.itemid) then
        return true
    end

    local items = {}
    local hotaItem
    for i = 1, #hotaItems do
        hotaItem = player:getItemById(hotaItems[i], true)
        if not hotaItem then
            return true
        end

        items[#items + 1] = hotaItem
    end


    for i = 1, #items do
        items[i]:remove()
    end
    player:setStorageValue(Storage.Hota, 1)
    player:addItem(HotaCompleted, 1)
    player:getPosition():sendMagicEffect(CONST_ME_STUN)
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You received '.. itemType:getName() .. ' on your backpack.')

    return true
end
 
Back
Top