• 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.2 receive random item from box

Lopaskurwa

Active Member
Joined
Oct 6, 2017
Messages
873
Solutions
2
Reaction score
49
Hello so i found this system which gives you random item when you click on it
Lua:
local presents = {
    [6570] = { -- 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 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)
    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end

How to send a message what item you got from that box. Example
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You got ' ...ITEMNAME... ' ")
 
Solution
gift:getName() won't work because gift is just itemid.
It should be ItemType(gift):getName()
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You got ".. (count ~= nil and count .. ' ' or '') .. ItemType(gift):getName())
Not tested but this should work:
Lua:
local presents = {
    [6570] = { -- 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 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 "..gift:getName().."")

    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end
 
Not tested but this should work:
Lua:
local presents = {
    [6570] = { -- 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 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 "..gift:getName().."")

    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end
Attempt to index local 'gift' <a number value> line 20.
 
gift:getName() won't work because gift is just itemid.
It should be ItemType(gift):getName()
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You got ".. (count ~= nil and count .. ' ' or '') .. ItemType(gift):getName())
 
Solution
Back
Top