• 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!

Random item box tfs 1.4

vexler222

Active Member
Joined
Apr 22, 2012
Messages
714
Solutions
15
Reaction score
46
Hi, i found this script for random item from box, but i don't know how to fix it, problem is when from box we got item with X count, i got error nil value count
For items with count 1, not more, it working normally

Code:
local presentbox = Action()
local presents = {
    [10503] = { -- item id of the item that will give u random items
        {2687, 10}, {6394, 3}, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114 --Setup the rewards here
    }
}

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

    local gift = targetItem[math.random(#targetItem)]
    if type(gift) == "table" then
        gift = gift[1]
        count = gift[2]
    end

    player:addItem(gift, count)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You got ".. ItemType(gift):getName() ..".")

    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end

presentbox:id(10503)
presentbox:register()
 
Solution
Hi, i found this script for random item from box, but i don't know how to fix it, problem is when from box we got item with X count, i got error nil value count
For items with count 1, not more, it working normally

Code:
local presentbox = Action()
local presents = {
    [10503] = { -- item id of the item that will give u random items
        {2687, 10}, {6394, 3}, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114 --Setup the rewards here
    }
}

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

    local gift = targetItem[math.random(#targetItem)]
    if type(gift) == "table" then...
Hi, i found this script for random item from box, but i don't know how to fix it, problem is when from box we got item with X count, i got error nil value count
For items with count 1, not more, it working normally

Code:
local presentbox = Action()
local presents = {
    [10503] = { -- item id of the item that will give u random items
        {2687, 10}, {6394, 3}, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114 --Setup the rewards here
    }
}

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

    local gift = targetItem[math.random(#targetItem)]
    if type(gift) == "table" then
        gift = gift[1]
        count = gift[2]
    end

    player:addItem(gift, count)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You got ".. ItemType(gift):getName() ..".")

    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end

presentbox:id(10503)
presentbox:register()

gift is being over-written, and then trying to be assigned as count afterwords.
This causes a nil value, because the table position is wrong.
Lua:
        gift = gift[1]
        count = gift[2]
the solution is to just swap their position, so that the table position is correct for count, and then over-written for gift.
Lua:
        count = gift[2]
        gift = gift[1]

Here is the script with the changes.
Lua:
local presentbox = Action()
local presents = {
    [10503] = { -- item id of the item that will give u random items
        {2687, 10}, {6394, 3}, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114 --Setup the rewards here
    }
}

function presentbox.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem = presents[item.itemid]
    if not targetItem then
        return true
    end
    
    local count = 1
    local gift = targetItem[math.random(#targetItem)]
    if type(gift) == "table" then
        count = gift[2]
        gift = gift[1]
    end
    
    player:addItem(gift, count)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You got ".. ItemType(gift):getName() ..".")
    
    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end

presentbox:id(10503)
presentbox:register()
 
Solution
Back
Top