• 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 Script random

hasbro

Member
Joined
Feb 15, 2009
Messages
286
Reaction score
6
I use key on the bookcase and dont work..no have any error.
LUA:
local table = {
    [15470] = {bookId=15429,chance=10,keyBreak=5},
    [15470] = {bookId=15427,chance=20,keyBreak=10},
    [15470] = {bookId=15428,chance=30,keyBreak=15}
}
 
function onUse(cid, item, fromPos, itemEx, toPos)
    for k, v in pairs(table) do
        if itemEx.itemid == k then
            if(math.random(100) <= v.keyBreak) then
                doRemoveItem(item.uid, 1)
            end
            if math.random(100) <= v.chance then
                doPlayerAddItem(cid, v.bookId)
            end
        end
    end
    return true
end
 
LUA:
local table = {
    {bookCase = 15470, bookId = 15429, chance = 10, keyBreak = 5},
    {bookCase = 15470, bookId = 15427, chance = 20, keyBreak = 10},
    {bookCase = 15470, bookId = 15428, chance = 30, keyBreak = 15}
}
 
function onUse(cid, item, fromPos, itemEx, toPos)
    for _, info in pairs(table) do
        if itemEx.itemid == info.bookCase then
            if(math.random(100) <= info.keyBreak) then
                doRemoveItem(item.uid)
            end
            if math.random(100) <= info.chance then
                doPlayerAddItem(cid, info.bookId)
            end
        end
    end
    return true
end

By putting [15470] three times you are using the same index all the time, which will result in the table only containing the last line.
 
Does it depend on a key which book to give?

or should it just give 1 book:
LUA:
local table = {
    {bookCase = 15470, bookId = 15429, chance = 10, keyBreak = 5},
    {bookCase = 15470, bookId = 15427, chance = 20, keyBreak = 10},
    {bookCase = 15470, bookId = 15428, chance = 30, keyBreak = 15}
}
 
function onUse(cid, item, fromPos, itemEx, toPos)
    for _, info in pairs(table) do
        if itemEx.itemid == info.bookCase then
            if(math.random(100) <= info.keyBreak) then
                doRemoveItem(item.uid)
                return true
            end
            if math.random(100) <= info.chance then
                doPlayerAddItem(cid, info.bookId)
                return true
            end
        end
    end
    return true
end
 
Yes, after player gain the 3 books him can't use the key in bookcase more..

- - - Updated - - -

I put this code, but is not good because each book him receive script +1 storage and if the player use key on the same bookcase the script add +1 storage..
LUA:
local table = {
    [15470] = {bookId=15429,chance=10,keyBreak=15},
    [15471] = {bookId=15427,chance=20,keyBreak=25},
    [15469] = {bookId=15428,chance=30,keyBreak=35}
}
 
function onUse(cid, item, fromPos, itemEx, toPos)
local storageplus = getPlayerStorageValue(cid, 62343) + 1
    for k, v in pairs(table) do
        if itemEx.itemid == k then
            if(math.random(100) <= v.keyBreak) then
                doRemoveItem(item.uid, 1)
				doPlayerSendTextMessage(cid, 21, "You turn the key in the small lock at the side of the shelf and... the key breaks.Other than that absolutely nothing happens.")
                  return true
            end
            if math.random(100) <= v.chance then
            if getPlayerStorageValue(cid,62343) <= 3 then
                doPlayerAddItem(cid, v.bookId)
				doRemoveItem(item.uid, 1)
		        doPlayerSendTextMessage(cid, 21, "A hidden stash appears the very moment you turn the key.Unfortunately the key breaks as you attempt to remove it from the lock.")				
               setPlayerStorageValue(cid, 62343, storageplus)                 
				 return true
            else
            doPlayerSendTextMessage(cid, 21, "You\'re not allowed")
            end
    end
    return true
end
end
end

- - - Updated - - -

Bump
 
Last edited:
Back
Top