• 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 TFS 1.3: Object that when used, gives several items as reward with a random chance

Claiom Solais

New Member
Joined
Nov 19, 2015
Messages
5
Reaction score
2
Hello, I would like to ask for help with a script. Lets say I have an Orshabaal's brain (item ID 5808), and when you use it, it dissapears and gives several items; each with an individual chance of being awarded. Let's say that there is a 100% chance of giving up to 5 crystal coins (ID 2160), 60% of giving a magic sword (ID 2400), a 40% of giving a magic plate armor (ID 2472), and 20% of giving a great shield (ID 2522).

Hopefully someone can help me with this, thanks.
 
Solution
Lua:
local items = {
    [5808] = { -- Orshabaals brain
        {id = 2160, count = 5, chance = 100}, --Crystal Coin
        {id = 2400, count = 1, chance = 60}, -- Magic Sword
        {id = 2472, count = 1, chance = 40}, -- Magic Plate Armor
        {id = 2522, count = 5, chance = 20}, -- Great Shield      
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local item_used, chance = items[item:getId()]
    if item_used then
        for i = 1, #item_used do
            chance = item_used[i].chance >= math.random(1, 100)
            if item_used[i].id and chance then
                player:addItem(item_used[i].id, item_used[i].count)
            end
        end
    end
    item:remove()
    return true...
Lua:
local items = {
    [5808] = { -- Orshabaals brain
        {id = 2160, count = 5, chance = 100}, --Crystal Coin
        {id = 2400, count = 1, chance = 60}, -- Magic Sword
        {id = 2472, count = 1, chance = 40}, -- Magic Plate Armor
        {id = 2522, count = 5, chance = 20}, -- Great Shield      
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local item_used, chance = items[item:getId()]
    if item_used then
        for i = 1, #item_used do
            chance = item_used[i].chance >= math.random(1, 100)
            if item_used[i].id and chance then
                player:addItem(item_used[i].id, item_used[i].count)
            end
        end
    end
    item:remove()
    return true
end

should work
 
Last edited:
Solution
Lua:
local items = {
    [5808] = { -- Orshabaals brain
        {id = 2160, count = 5, chance = 100}, --Crystal Coin
        {id = 2400, count = 1, chance = 60}, -- Magic Sword
        {id = 2472, count = 1, chance = 40}, -- Magic Plate Armor
        {id = 2522, count = 5, chance = 20}, -- Great Shield     
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local item_used, chance = items[item:getId()]
    if item_used then
        for i = 1, #item_used do
            chance = item_used[i].chance >= math.random(1, 100)
            if item_used[i].id and chance then
                player:addItem(item_used[i].id, item_used[i].count)
            end
        end
    end
    item:remove()
    return true
end

should work
I wouldn't remove the item, unless the item is in the table, but otherwise, this looks good. :)
 
How can I limit to X amount (like 1 or 2) item(s) from the list? Like only one/two/..., with various chances (i mean most chance for coins, but also has a chance to give some item). I know how to set percentages, but want to mention that to prevent getting the code that just gives one random item with the same chances for each.

Btw. when i tried to edit this code like this:
Code:
local items = {
    [11244] = { -- Mino BP
        {id = 12248, count = 1, chance = 20}, -- Mino Horn
        {id = 5878, count = 1, chance = 20}, -- Mino Leather
        {id = 2152, count = 3, chance = 20}, -- 3 Platinum Coins 
        {id = 2387, count = 1, chance = 20}, -- Double Axe      
    }
}

Sometimes no item was given (which is understandable, just each item haven't hit the chance), but also sometimes stone pebble floor (id 22163) was spawned ;)
 
How can I limit to X amount (like 1 or 2) item(s) from the list? Like only one/two/..., with various chances (i mean most chance for coins, but also has a chance to give some item). I know how to set percentages, but want to mention that to prevent getting the code that just gives one random item with the same chances for each.

Btw. when i tried to edit this code like this:
Code:
local items = {
    [11244] = { -- Mino BP
        {id = 12248, count = 1, chance = 20}, -- Mino Horn
        {id = 5878, count = 1, chance = 20}, -- Mino Leather
        {id = 2152, count = 3, chance = 20}, -- 3 Platinum Coins
        {id = 2387, count = 1, chance = 20}, -- Double Axe  
    }
}

Sometimes no item was given (which is understandable, just each item haven't hit the chance), but also sometimes stone pebble floor (id 22163) was spawned ;)
This should do the trick.

It guarantee's a maximum amount of items, but does not guarantee a minimum amount.

So you can get 0 items, 1 item, or 2 items, but never 3 or 4.

Lua:
local itemList = {
    [11244] = { -- Mino BP
        max_items = 2, loot = {
            {id = 12248, count = 1, chance = 20}, -- Mino Horn
            {id = 5878, count = 1, chance = 20}, -- Mino Leather
            {id = 2152, count = 3, chance = 20}, -- 3 Platinum Coins 
            {id = 2387, count = 1, chance = 20} -- Double Axe
        }
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local item_used = itemList[item:getId()]
    if item_used then
        local items, chance = {}, 0
        for i = 1, #item_used.loot do
            chance = math.random(100)
            if chance <= item_used.loot[i].chance then
                items[#items + 1] = {item_used.loot[i].id, item_usedloot[i].count}
            end
        end
        if #items > item_used.max_items then
            while #items > item_used.max_items do
                table.remove(items, math.random(#items))
            end
        end
        for i = 1, #items do
            player:addItem(items[i][1], items[i][2])
        end
        item:remove()
    end
    return true
end
 
Back
Top