• 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 Doubt in Programming

hasbro

Member
Joined
Feb 15, 2009
Messages
286
Reaction score
6
Hello,
I beginner in LUA, but i try everyday create new scripts to trainer my skill, but i creating one script appears a doubt..I creating tibiarl script, and this script talk about 3 items where you use a key on 1 item and you can or not receive one book, but have a change of broken your key, and you try again use key in other item..
I dont now create a random chance about the key..and the 3 books you receive on final ..
 
I am sorry, I did not get what you where telling us.
Are you looking for a way to create a script with a random chance? Maybe explain your problem again in one sentence.
 
le me see if I can get this right:
You are trying to make a script that you use a key on an item and there's a % to get a book, but everytime you use the key, it has % to break the key

is that it?
 
LUA:
local table = {
[itemidonwhatyouusethekey] = {bookId=bookitemid,chance=},
[itemidonwhatyouusethekey] = {bookId=bookitemid,chance=},
[itemidonwhatyouusethekey] = {bookId=bookitemid,chance=}
}
function onUse(cid, item, fromPos, itemEx, toPos)
for k, v in pairs(table) do
if itemEx.itemid == k then
local mathChance = math.random(100)
if mathChance <= v.chance then
doPlayerAddItem(cid, v.bookId)
end
end
end
return true
end


You said the script should be use key on 3 different items (itemidonwhatyouusethekey) and have chance (chance) to get 3 different books (bookId) depending on the item in which you used the key....

Change:
itemidonwhatyouusethekey for the 3 different items id
bookId for the itemid of the 3 different books
chance for the chance in percent that you have to get the book

Like this:
[2160] = {bookId=1968, chance=1}


Hope it works and you realize how it does....
 
@SUMM
Yes have a 3 items you use the key and have chance for you gain a book of 3 or the key break but you never gain the same book..
Arquivo:Lagatos1.png - Tibia Wiki
@Scarlet Ayleid
Yes , it is correct but you never gain the same book..
@Dantarrix
Ohhh good , i never think create this script like you..but i put the function to break the key?

Can someone explain to me about this function..?
for k, v in pairs(table) do
I dont understand this function..
 
for k, v in pairs(table) do

in this case, "k" will be the key used to index the table while "v" is the sub-table

LUA:
local table = {
[1] = {bookId=123,chance=50},
[2] = {bookId=456,chance=60},
[3] = {bookId=789,chance=70}
}
first loop: k = 1 | v = {bookId=123,chance=50}
second loop: k = 2 | v = {bookId=456,chance=60}
etc..
 
LUA:
local table = {
    [itemidonwhatyouusethekey] = {bookId=bookitemid,chance=},
    [itemidonwhatyouusethekey] = {bookId=bookitemid,chance=},
    [itemidonwhatyouusethekey] = {bookId=bookitemid,chance=}
}
local keyBreak = 25

function onUse(cid, item, fromPos, itemEx, toPos)
    for k, v in pairs(table) do
        if itemEx.itemid == k then
            if(math.random(1, 100) <= keyBreak) then
                doRemoveItem(item.uid, 1)
            elseif math.random(1,100) <= v.chance then
                doPlayerAddItem(cid, v.bookId)
            end
        end
    end
    return true
end
something like this
 
@All: I dont know why I missed the break chance... I think that maybe you want it to be different deppending on each itemEx... So:

LUA:
local table = {
    [itemidonwhatyouusethekey] = {bookId=bookitemid,chance=,keyBreak=},
    [itemidonwhatyouusethekey] = {bookId=bookitemid,chance=,keyBreak=},
    [itemidonwhatyouusethekey] = {bookId=bookitemid,chance=,keyBreak=}
}
 
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

@Scarlet: math.random(100) = math.random(1,100) xDDD
if you do this:
LUA:
if(math.random(1, 100) <= keyBreak) then
                doRemoveItem(item.uid, 1)
            elseif math.random(1,100) <= v.chance then
                doPlayerAddItem(cid, v.bookId)
            end
the key will break or add the item, so never both at the same time (add book and break key...)

Only making you advice, dont take it bad please...

Sorry all for bad english...
 
Back
Top