If I were to per-say kill a mob, and I had a 20% chance of getting a uncommon item, how would I go about scripting that? I was thinking creating a random number from 1-5 and if it landed on 3 you would get the item. Is there a better more efficient and detailed way to do this? There are no videos on free models or guides or anything about random drop rates so I'm a little stuck and that's the only thing I can think of
So, you want to make loot tables? Too bad, I'm telling you anyway. Loot tables are simple enough - Consider a table where every key is the unique loot, and every value is the probability of it being dropped (relative to the others)
1 | Loot = { |
2 | Loot 1 = 1 ; |
3 | Loot 2 = 3 ; |
4 | Loot 3 = 10 ; |
5 | ReallyFarTooCommonLoot = 1000 ; |
6 | } |
Then, we can generate that table into an array of values:
1 | newLoot = { } |
2 | for k,v in next , Loot do |
3 | for i = 1 , v do |
4 | newLoot [ #newLoot+ 1 ] = k |
5 | end |
6 | end |
7 | Loot = newLoot; |
And then we can randomly select an entry from that loot table array.
1 | Drop = Loot [ math.random(#Loot) ] |
And now you have your chosen drop, with a weighted chance of selection.
This is just an idea on how you could go about doing this
So you could use a random number and have a table and based on the random number, one of the items in the table is chosen.
1 | local items = { "common" , "common" , "common" , "common" , "uncommon" , "common" , "common" , |
2 | "common" , "common" , "uncommon" , "rare" } |
Then say whatever happens, and they are going to get the item
1 | local items = { "common" , "common" , "common" , "common" , "uncommon" , "common" , "common" , |
2 | "common" , "common" , "uncommon" , "rare" } |
3 | local chosen = items [ math.random( 1 ,#items) ] -- gets a random one from the list |
4 | print (chosen) |
Then just if the item is common then whatever, elseif it is uncommon then whatever, elseif it is rare then whatever.
01 | local items = { "common" , "common" , "common" , "common" , "uncommon" , "common" , "common" , |
02 | "common" , "common" , "uncommon" , "rare" } |
03 | local chosen = items [ math.random( 1 ,#items) ] -- gets a random one from the list |
04 | print (chosen) |
05 | if chosen = = "common" then |
06 | --Do whatever you want |
07 | elseif chosen = = "uncommon" then |
08 | --Do whatever |
09 | elseif chosen = = "rare" then |
10 | --Do whatever |
11 | end |
If I have helped answer your question, please remember to accept my answer. Otherwise feel free to comment or message me on how I could have done better to help you. :)