• 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] Prize Box (By % Chance)

Athenuz

Owlz!
Joined
Oct 1, 2015
Messages
234
Reaction score
27
Location
México
Hello,

Can someone help me with this action script please?

When using an item, the item gives a random item according to % chance and remove the used item on-use?
With possibility of configure the % of chance like:

100%: gold coin
40%: platinum coin
10%: crystal coin
1%: demon armor
0.5%: demon legs

Regards
 
Hello,

Can someone help me with this action script please?

When using an item, the item gives a random item according to % chance and remove the used item on-use?
With possibility of configure the % of chance like:

100%: gold coin
40%: platinum coin
10%: crystal coin
1%: demon armor
0.5%: demon legs

Regards

100% probability that does not make sense in a statement if you have other options the one that will always be picked is going to be gold coin.
 
Hello,

Can someone help me with this action script please?

When using an item, the item gives a random item according to % chance and remove the used item on-use?
With possibility of configure the % of chance like:

100%: gold coin
40%: platinum coin
10%: crystal coin
1%: demon armor
0.5%: demon legs

Regards
Code:
local prize = {
    ['100'] = { ITEM_GOLD_COIN, amount = 1 },
    ['40'] = { ITEM_PLATINUM_COIN, amount = 1 },
    ['10'] = { ITEM_CRYSTAL_COIN, amount = 1 },
    ['1'] = { 2494, amount = 1 }, --demon armor
    ['0.5'] = { 2495, amount = 1 } -- demon legs
}   

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local number = math.random() * 100
    local chance = tostring( number < 1 and 0.5 or number )
    if prize[chance] then
        item:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:addItem(prize[chance][1], prize[chance].amount)
        item:remove()
    end
    return true
end
 
Code:
local prize = {
    ['100'] = { ITEM_GOLD_COIN, amount = 1 },
    ['40'] = { ITEM_PLATINUM_COIN, amount = 1 },
    ['10'] = { ITEM_CRYSTAL_COIN, amount = 1 },
    ['1'] = { 2494, amount = 1 }, --demon armor
    ['0.5'] = { 2495, amount = 1 } -- demon legs
}  

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local number = math.random() * 100
    local chance = tostring( number < 1 and 0.5 or number )
    if prize[chance] then
        item:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:addItem(prize[chance][1], prize[chance].amount)
        item:remove()
    end
    return true
end

Thanks Codex, I'll test right now
 
the issue is that you can't determine that this item has 40% chance that item has 70%, at least thats what I think, I don't see anyway to make that

Edit: maybe if you source edit sure but I doubt u can do this it Lua
 
both items are giving random items not items by chance

suprise bag:

Code:
local config = {
    [6570] = { -- blue present
        {2687, 10}, {6394, 3}, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114
    },
    [6571] = { -- red present
        {2152, 10}, {2152, 10}, {2152, 10}, 2153, 5944, 2112, 6568, 6566, 2492, 2520, 2195, 2114, 2114, 2114, 6394, 6394, 6576, 6576, 6578, 6578, 6574, 6574
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local count = 1
    local gift = present[math.random(#present)]
    if type(gift) == "table" then
        count = gift[2]
        gift = gift[1]
    end

    player:addItem(gift, count)
    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end
 
I'm not 100% sure if that's what you wanted but you could try this:
Code:
local prize = {
    [1] = {chance = 5, id = 2495, amount = 1 },
    [2] = {chance = 10, id = 2494, amount = 1 },
    [3] = {chance = 25, id = ITEM_CRYSTAL_COIN, amount = 1 },
    [4] = {chance = 50, id = ITEM_PLATINUM_COIN, amount = 1 },
    [5] = {chance = 100, id = ITEM_GOLD_COIN, amount = 1 },
}

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

    for i = 1,#prize do
    local number = math.random() * 100
    if prize[i].chance>100-number then
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:addItem(prize[i].id, prize[i].amount)
        item:remove()
    end
    end
    return true
end

It will random every item eventually based on its chance, possible to obtain multiple items. If you want to make sure that only one item can be obtained then move
Code:
local number = math.random() * 100
in front of the loop and add
Code:
break
after
Code:
item:remove()
 
@Lava Titan
Why would math be forbidden in lua?

https://github.com/orts/server/blob...bf/data/actions/scripts/tools/rustremover.lua
This script does stuff by chances, although it is ugly it might help you implement that, should not be much work.

Totally forgot it!, Thanks Summ!


I'm not 100% sure if that's what you wanted but you could try this:
Code:
local prize = {
    [1] = {chance = 5, id = 2495, amount = 1 },
    [2] = {chance = 10, id = 2494, amount = 1 },
    [3] = {chance = 25, id = ITEM_CRYSTAL_COIN, amount = 1 },
    [4] = {chance = 50, id = ITEM_PLATINUM_COIN, amount = 1 },
    [5] = {chance = 100, id = ITEM_GOLD_COIN, amount = 1 },
}

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

    for i = 1,#prize do
    local number = math.random() * 100
    if prize[i].chance>100-number then
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:addItem(prize[i].id, prize[i].amount)
        item:remove()
    end
    end
    return true
end

It will random every item eventually based on its chance, possible to obtain multiple items. If you want to make sure that only one item can be obtained then move
Code:
local number = math.random() * 100
in front of the loop and add
Code:
break
after
Code:
item:remove()


Testing... :D
 
Code:
local prize = {
    [1] = {chance = 1, id = 2495, amount = 1 },
    [2] = {chance = 2, id = 2494, amount = 1 },
    [3] = {chance = 50, id = ITEM_CRYSTAL_COIN, amount = 1 },
    [4] = {chance = 50, id = ITEM_PLATINUM_COIN, amount = 1 },
    [5] = {chance = 90, id = ITEM_GOLD_COIN, amount = 1 },
}

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

    for i = 1,#prize do local number = math.random() * 100
    if prize[i].chance>100-number then
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:addItem(prize[i].id, prize[i].amount)
        item:remove()
        break
    end
    end
    return true
end

I hope this way is the right one to give only 1 item insteda of multiple items...

also tried
Code:
item:remove(1)
To remove 1 x 1 item (since this item may be accumulated) as coins or gems...

Thanks a lot for the help
 
Back
Top