• 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!

Random Vocation

Koozaczek

New Member
Joined
Jul 19, 2008
Messages
53
Reaction score
0
Hello,
Could somebody do script to my project?
It should work that, when i click on item "x" then i got random vocation for example one from 10 possible.
Also i have % chance to get this voc for example i have 30% to roll sorcerer, 20% druid, 5% assasin etc.
Also will be great that this item will be from 30 lvl.

Somebody can do this?
Please for help.
 
Code:
local voc_chance = {
   [1] = {min = 1, max = 10, voc_id = 1111}, -- 10 percent chance
   [2] = {min = 11, max = 40, voc_id = 1111}, -- 30 percent chance
   [3] = {min = 41, max = 100, voc_id = 1111} -- 60 percent chance
}

function onUse(cid, item, frompos, item2, topos)
   -- check player level here
   local number = math.random(1,100)   
   for i = 1, #voc_chance do
     if number >= voc_chance[i].min and number <= voc_chance[i].max then
       -- change vocation here
       break
     end
   end
   return true
end
 
Last edited:
Its dont work, i click and nothing happend.
Also dont have any error in console, i add this script to actions.

PS. Tfs 0.3.6

Maybe i do sometching wrong? Please help.
 
seriously... read the fuc...........................................................................................................
sigh.
I'll cut the script down so you can understand.

This section describes the chance for each vocation.

Code:
local voc_chance = {
    [1] = {min = 1, max = 10, voc_id = 1111}, -- 10 percent chance
    [2] = {min = 11, max = 40, voc_id = 1111}, -- 30 percent chance
    [3] = {min = 41, max = 100, voc_id = 1111} -- 60 percent chance
}
For each vocation add another line
Code:
[1] =
[2] =
[3] =
[4] =
When setting up the chance for the vocations the chance is out of 100.
So you use the min and max values to set the percentage.

Code:
min = 1, max = 1, -- 1 percent chance.
min = 2, max = 11, -- 10 percent chance.
min = 11, max = 30, -- 29 percent chance.
min = 31, max = 96, -- 66 percent chance
min = 97, max = 100, -- 4 percent chance.
When calculating the chance, count each number.. from min to max.
Code:
min = 97, max = 100, -- 4 percent chance.
Code:
 97, 98, 99, 100
The last part is the vocation ID. If you have a standard server it would look like this. Knight, Paladin, Sorcerer, Druid.. 1,2,3,4 respectively.
Code:
 voc_id = 1111},
Always make sure the last line has no comma.
Code:
voc_id = 1},
voc_id = 2},
voc_id = 3},
voc_id = 4}
This part tells you what kind of script it is and how it can be used. It describes it as a onUse function. So using an object/wall/lever/item can all be used.
Code:
function onUse(cid, item, frompos, item2, topos)
I never added it in, but this is where you'd want to check for the players level.
Code:
-- check player level here
If you wanted to check for players level you'd want to add these lines.
Code:
if not getPlayerLevel(cid) >= 30 then
   return false
end
This part runs a randomiser. It rolls a number 1-100 randomly.
Code:
local number = math.random(1,100)
This part is a loop. It will loop through the table attempting to find out what number was rolled by the randomiser, then end itself.
Code:
for i = 1, #voc_chance do
    if number >= voc_chance[i].min and number <= voc_chance[i].max then
       -- change vocation here
       break
    end
end
This part shows to start the loop going through the table voc_chance.
Code:
for i = 1, #voc_chance do
This line checks if the number chosen by the randomiser is within the min and max of each line.
If the number chosen was 34, and the first line only checks for numbers "min = 1, max = 30," then it would continue to the next line, because it is false.

Code:
 if number >= voc_chance[i].min and number <= voc_chance[i].max then
Here is where you change the vocation of the player. Once again, I did not add it in.
Code:
  -- change vocation here
If you were to add it in, you would want to add this line.
Code:
doPlayerSetVocation(cid, voc_chance[i].voc_id)
The above changing player vocation, and then this line.. only happens if the if statement returns true. Using break stops the loop, so it doesn't need to keep checking.
Code:
break
Next part is ending the if statement, and ending the loop.
Code:
   end
end
This part returns that the onUse function ended correctly and also ends it effectively.
Code:
    return true
end
Coming back full circle, I am going to change it as you said you had 10 different vocations.
I swear to god if you come back and say it doesn't work I'm going to commit sepuku.

Code:
local voc_chance = {
  [1] = {min = 1, max = 10, voc_id = 1},
  [2] = {min = 11, max = 20, voc_id = 2},
  [3] = {min = 21, max = 30, voc_id = 3},
  [4] = {min = 31, max = 40, voc_id = 4},
  [5] = {min = 41, max = 50, voc_id = 5},
  [6] = {min = 51, max = 60, voc_id = 6},
  [7] = {min = 61, max = 70, voc_id = 7},
  [8] = {min = 71, max = 80, voc_id = 8},
  [9] = {min = 81, max = 90, voc_id = 9},
  [10] = {min = 91, max = 100, voc_id = 10}
}
local player_level = 30

function onUse(cid, item, frompos, item2, topos)
   if not getPlayerLevel(cid) >= player_level then
     return false
   end
   local number = math.random(1,100)
   for i = 1, #voc_chance do
     if number >= voc_chance[i].min and number <= voc_chance[i].max then
       doPlayerSetVocation(cid, voc_chance[i].voc_id)
       break
     end
   end
   return true
end
By the way, the script worked exactly how it was scripted to start.
The fact you got no error means it was scripted correctly.

Cheers,

Xikini
 
Last edited:
Thx that you explain me step by step but i have error when i use item:

[19/06/2015 00:04:45] [Error - Action Interface]
[19/06/2015 00:04:45] data/actions/scripts/voc.lua:eek:nUse
[19/06/2015 00:04:45] Description:
[19/06/2015 00:04:45] data/actions/scripts/voc.lua:16: attempt to compare number with boolean
[19/06/2015 00:04:45] stack traceback:
[19/06/2015 00:04:45] data/actions/scripts/voc.lua:16: in function <data/actions/scripts/voc.lua:15>
 
Thx that you explain me step by step but i have error when i use item:

[19/06/2015 00:04:45] [Error - Action Interface]
[19/06/2015 00:04:45] data/actions/scripts/voc.lua:eek:nUse
[19/06/2015 00:04:45] Description:
[19/06/2015 00:04:45] data/actions/scripts/voc.lua:16: attempt to compare number with boolean
[19/06/2015 00:04:45] stack traceback:
[19/06/2015 00:04:45] data/actions/scripts/voc.lua:16: in function <data/actions/scripts/voc.lua:15>
change
Code:
if not getPlayerLevel(cid) >= player_level then
to this.
Code:
if getPlayerLevel(cid) < player_level then

also.. if you want the item to be removed after using it..
add this
Code:
doRemoveItem(item.uid, 1)
in the middle of these two
Code:
  doPlayerSetVocation(cid, voc_chance[i].voc_id)
  doRemoveItem(item.uid, 1) -------------------------- right here
  break
 
Last edited:
Thx very much, now works fine.
Also can you tell me how to add if i have already vocation then i cant use this item ?

Sorry that i ask for help in everything but im noob in lua and i cant create scripts alone.
 
Thx very much, now works fine.
Also can you tell me how to add if i have already vocation then i cant use this item ?

Sorry that i ask for help in everything but im noob in lua and i cant create scripts alone.
Add another if statement.
(Btw this is kind of redundant.. if you change players vocations around constantly then their mana/hp/capacity are going to be screwed to hell.)
(If they are a rook character, just don't add that ID)
Code:
   if number >= voc_chance[i].min and number <= voc_chance[i].max then
     if getPlayerVocation(cid) == voc_chance[i].voc_id then
       doPlayerSendCancel(cid, "Random selection chose the same vocation. Try again.")
     else
       doPlayerSetVocation(cid, voc_chance[i].voc_id)
       doRemoveItem(item.uid, 1)
       doPlayerSendTextMessage(cid,22,"Your vocation has been changed to " .. getPlayerVocation(cid) ..".")
     end
     break
   end
   return true
end

--edit
You may have to call it a bit differently..
If that text message doesn't work then use this
Code:
getVocationInfo(getPlayerVocation(cid)).name
 
Last edited:
Message works fine but now all time choose vocation number 1
Here is the entire script.
Code:
local voc_chance = {
  [1] = {min = 1, max = 10, voc_id = 1},
  [2] = {min = 11, max = 20, voc_id = 2},
  [3] = {min = 21, max = 30, voc_id = 3},
  [4] = {min = 31, max = 40, voc_id = 4},
  [5] = {min = 41, max = 50, voc_id = 5},
  [6] = {min = 51, max = 60, voc_id = 6},
  [7] = {min = 61, max = 70, voc_id = 7},
  [8] = {min = 71, max = 80, voc_id = 8},
  [9] = {min = 81, max = 90, voc_id = 9},
  [10] = {min = 91, max = 100, voc_id = 10}
}
local player_level = 30

function onUse(cid, item, frompos, item2, topos)  
   if getPlayerLevel(cid) < player_level then
     return false
   end
   local number = math.random(1,100)
   for i = 1, #voc_chance do
   if number >= voc_chance[i].min and number <= voc_chance[i].max then
     if getPlayerVocation(cid) == voc_chance[i].voc_id then
       doPlayerSendCancel(cid, "Random selection chose the same vocation. Try again.")
     else
       doPlayerSetVocation(cid, voc_chance[i].voc_id)
       doRemoveItem(item.uid, 1)
       doPlayerSendTextMessage(cid,22,"Your vocation has been changed to " .. getPlayerVocation(cid) ..".")
     end
     break
   end
   return true
end
This below script is slightly altered, to see what randomization will happen.
When you use the below script you should get messages telling you the number that was rolled.. or if it's the same vocation.
If you see only the number one appearing, then let me know.
Code:
local voc_chance = {
  [1] = {min = 1, max = 10, voc_id = 1},
  [2] = {min = 11, max = 20, voc_id = 2},
  [3] = {min = 21, max = 30, voc_id = 3},
  [4] = {min = 31, max = 40, voc_id = 4},
  [5] = {min = 41, max = 50, voc_id = 5},
  [6] = {min = 51, max = 60, voc_id = 6},
  [7] = {min = 61, max = 70, voc_id = 7},
  [8] = {min = 71, max = 80, voc_id = 8},
  [9] = {min = 81, max = 90, voc_id = 9},
  [10] = {min = 91, max = 100, voc_id = 10}
}
local player_level = 30

function onUse(cid, item, frompos, item2, topos)  
   if getPlayerLevel(cid) < player_level then
     return false
   end
   local number = math.random(1,100)
   for i = 1, #voc_chance do
   if number >= voc_chance[i].min and number <= voc_chance[i].max then
     if getPlayerVocation(cid) == voc_chance[i].voc_id then
       doPlayerSendCancel(cid, "Random selection chose the same vocation. Try again.")
     else
       -- doPlayerSetVocation(cid, voc_chance[i].voc_id)
       -- doRemoveItem(item.uid, 1)
       -- doPlayerSendTextMessage(cid,22,"Your vocation has been changed to " .. getPlayerVocation(cid) ..".")
       doPlayerSendTextMessage(cid,22,"".. [i] .."")
     end
     break
   end
   return true
end
 
seriously... read the fuc...........................................................................................................
sigh.
I'll cut the script down so you can understand.

This section describes the chance for each vocation.

Code:
local voc_chance = {
    [1] = {min = 1, max = 10, voc_id = 1111}, -- 10 percent chance
    [2] = {min = 11, max = 40, voc_id = 1111}, -- 30 percent chance
    [3] = {min = 41, max = 100, voc_id = 1111} -- 60 percent chance
}
For each vocation add another line
Code:
[1] =
[2] =
[3] =
[4] =
When setting up the chance for the vocations the chance is out of 100.
So you use the min and max values to set the percentage.

Code:
min = 1, max = 1, -- 1 percent chance.
min = 2, max = 11, -- 10 percent chance.
min = 11, max = 30, -- 29 percent chance.
min = 31, max = 96, -- 66 percent chance
min = 97, max = 100, -- 4 percent chance.
When calculating the chance, count each number.. from min to max.
Code:
min = 97, max = 100, -- 4 percent chance.
Code:
 97, 98, 99, 100
The last part is the vocation ID. If you have a standard server it would look like this. Knight, Paladin, Sorcerer, Druid.. 1,2,3,4 respectively.
Code:
 voc_id = 1111},
Always make sure the last line has no comma.
Code:
voc_id = 1},
voc_id = 2},
voc_id = 3},
voc_id = 4}
This part tells you what kind of script it is and how it can be used. It describes it as a onUse function. So using an object/wall/lever/item can all be used.
Code:
function onUse(cid, item, frompos, item2, topos)
I never added it in, but this is where you'd want to check for the players level.
Code:
-- check player level here
If you wanted to check for players level you'd want to add these lines.
Code:
if not getPlayerLevel(cid) >= 30 then
   return false
end
This part runs a randomiser. It rolls a number 1-100 randomly.
Code:
local number = math.random(1,100)
This part is a loop. It will loop through the table attempting to find out what number was rolled by the randomiser, then end itself.
Code:
for i = 1, #voc_chance do
    if number >= voc_chance[i].min and number <= voc_chance[i].max then
       -- change vocation here
       break
    end
end
This part shows to start the loop going through the table voc_chance.
Code:
for i = 1, #voc_chance do
This line checks if the number chosen by the randomiser is within the min and max of each line.
If the number chosen was 34, and the first line only checks for numbers "min = 1, max = 30," then it would continue to the next line, because it is false.

Code:
 if number >= voc_chance[i].min and number <= voc_chance[i].max then
Here is where you change the vocation of the player. Once again, I did not add it in.
Code:
  -- change vocation here
If you were to add it in, you would want to add this line.
Code:
doPlayerSetVocation(cid, voc_chance[i].voc_id)
The above changing player vocation, and then this line.. only happens if the if statement returns true. Using break stops the loop, so it doesn't need to keep checking.
Code:
break
Next part is ending the if statement, and ending the loop.
Code:
   end
end
This part returns that the onUse function ended correctly and also ends it effectively.
Code:
    return true
end
Coming back full circle, I am going to change it as you said you had 10 different vocations.
I swear to god if you come back and say it doesn't work I'm going to commit sepuku.

Code:
local voc_chance = {
  [1] = {min = 1, max = 10, voc_id = 1},
  [2] = {min = 11, max = 20, voc_id = 2},
  [3] = {min = 21, max = 30, voc_id = 3},
  [4] = {min = 31, max = 40, voc_id = 4},
  [5] = {min = 41, max = 50, voc_id = 5},
  [6] = {min = 51, max = 60, voc_id = 6},
  [7] = {min = 61, max = 70, voc_id = 7},
  [8] = {min = 71, max = 80, voc_id = 8},
  [9] = {min = 81, max = 90, voc_id = 9},
  [10] = {min = 91, max = 100, voc_id = 10}
}
local player_level = 30

function onUse(cid, item, frompos, item2, topos)
   if not getPlayerLevel(cid) >= player_level then
     return false
   end
   local number = math.random(1,100)
   for i = 1, #voc_chance do
     if number >= voc_chance[i].min and number <= voc_chance[i].max then
       doPlayerSetVocation(cid, voc_chance[i].voc_id)
       break
     end
   end
   return true
end
By the way, the script worked exactly how it was scripted to start.
The fact you got no error means it was scripted correctly.

Cheers,

Xikini
Lol Hahahahhaha, Nice Bro ;)
 
Sory that i back to thread but i start my project again and i still have problem with this script.
at first please remove this
if number >= voc_chance.min and number <= voc_chance.max then
if getPlayerVocation(cid) == voc_chance.voc_id then
doPlayerSendCancel(cid, "Random selection chose the same vocation. Try again.")

from the script cause i dont want anymore to be used only one time .
Anyway i still have problem with last both scripts what you send me cause all the time choose just first vocation.
 
Sory that i back to thread but i start my project again and i still have problem with this script.
at first please remove this

from the script cause i dont want anymore to be used only one time .
Anyway i still have problem with last both scripts what you send me cause all the time choose just first vocation.
That would be a completely separate script.
In either case,
Code:
local storage = 45001
local n = 4 -- number of vocations in server.
            -- not including vocation '0', rookgaardian or promotions

function onUse(cid, item, fromPosition, itemEx, toPosition)
     if getPlayerStorageValue(cid, storage) == 1 then
         doPlayerSendCancel(cid, "Vocation already changed.")
         return true
     end
     local rand = math.random(1, n)
     doPlayerSetVocation(cid, rand)
     -- doRemoveItem(item.uid, 1)
     -- setPlayerStorageValue(cid, storage, 1)
     doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Your vocation has been changed to " .. getPlayerVocation(cid) ..".")
     return true
end
 
But i need the script with percent to each vocation its very important.


local voc_chance = {
[1] = {min = 1, max = 10, voc_id = 1},
[2] = {min = 11, max = 20, voc_id = 2},
[3] = {min = 21, max = 30, voc_id = 3},
[4] = {min = 31, max = 40, voc_id = 4},
[5] = {min = 41, max = 50, voc_id = 5},
[6] = {min = 51, max = 60, voc_id = 6},
[7] = {min = 61, max = 70, voc_id = 7},
[8] = {min = 71, max = 80, voc_id = 8},
[9] = {min = 81, max = 90, voc_id = 9},
[10] = {min = 91, max = 100, voc_id = 10}
}
local player_level = 30

function onUse(cid, item, frompos, item2, topos)
if not getPlayerLevel(cid) >= player_level then
return false
end
local number = math.random(1,100)
for i = 1, #voc_chance do
if number >= voc_chance.min and number <= voc_chance.max then
doPlayerSetVocation(cid, voc_chance.voc_id)
break
end
end
return true
end

This script almost work cause roll vocations fine, but the percent dosent work
 
But i need the script with percent to each vocation its very important.



This script almost work cause roll vocations fine, but the percent dosent work
Maybe because you didn't config the min, max for each vocation correctly. Post the table you're using
 
Back
Top