• 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 add chance to random items

zirra

Member
Joined
Jun 11, 2009
Messages
343
Solutions
1
Reaction score
7
Location
Arizona, Usa
So I don't wanna try adding a bunch of codes hoping it works, I've searched for this and came up empty already. I want to add chance to certain items. like a 1% chance for one item and the rest equally random. I know this is probably something easy but I can't think of which way to do it. For ex; Item 11640 needs to have a much rarer probability. near impossible. while the others are all random with equal chance.

Code:
local items = {{2152, 10}, {2394, 100}, {2391, 37}, {2392, 4}, {11640, 1}, {11641, 1}}
local min_level = 20    --Minimum level to pick up the items in the chest.
local time = 1440     --how many minutes to recive reward.
local storage = 91838
function onUse(cid, item, frompos, item2, topos)
    if getPlayerLevel(cid) >= min_level then
        if getPlayerStorageValue(cid, storage) < os.time() then
            local it = items[math.random(#items)]
            doPlayerAddItem(cid, it[1], it[2])
            doPlayerSendTextMessage(cid, 27, "You have received "..it[2].." "..getItemNameById(it[1])..". You can take your reward in "..time.." minutes.")
            setPlayerStorageValue(cid, storage, os.time() + time * 60)
        else
            return doPlayerSendCancel(cid, "You have already received your reward for today.")
        end
    else
        return doPlayerSendCancel(cid, "You do not have the required level to receive this reward ["..min_lv.."].")
    end
    return true
end
 
Code:
local items = {{2152, 10}, {2394, 100}, {2391, 37}, {2392, 4}, {11641, 1}}
local rareitems = {{11640, 1}} -- {itemid, count}
local rarechance = 1/100 -- 1 out of 100, 1%
local min_level = 20    --Minimum level to pick up the items in the chest.
local time = 1440     --how many minutes to recive reward.
local storage = 91838
function onUse(cid, item, frompos, item2, topos)
    if getPlayerLevel(cid) >= min_level then
        if getPlayerStorageValue(cid, storage) < os.time() then
            local rd = math.random(1, 100)
            if rd <= rarechance*100 then
                local it = rareitems[math.random(#rareitems)]
                doPlayerAddItem(cid, it[1], it[2])
                doPlayerSendTextMessage(cid, 27, "You have received "..it[2].." "..getItemNameById(it[1])..". You can take your reward in "..time.." minutes.")
                setPlayerStorageValue(cid, storage, os.time() + time * 60)
            else
                local it = items[math.random(#items)]
                doPlayerAddItem(cid, it[1], it[2])
                doPlayerSendTextMessage(cid, 27, "You have received "..it[2].." "..getItemNameById(it[1])..". You can take your reward in "..time.." minutes.")
                setPlayerStorageValue(cid, storage, os.time() + time * 60)
            end
        else
            return doPlayerSendCancel(cid, "You have already received your reward for today.")
        end
    else
        return doPlayerSendCancel(cid, "You do not have the required level to receive this reward ["..min_lv.."].")
    end
    return true
end

This will have 1% chance to give a random item from rareitems
 
Back
Top