So I want to make a chance perchance as you have in pets: Common, rare, epic, legendary and they have their own chance percentage so I wanted to make it with coins so it will pick coins after every 10 min it gives you random coins based on its chance percentage but I found nothing about this topic please help me out.
1 | 500 Coins - 95 % = Common |
2 | 1000 Coins - 85 % = Common |
3 | 2500 Coins - 75 % = Rare |
4 | 6000 Coins - 25 % = Epic |
5 | 10000 Coins - 10 % = Legendary |
Create a Variable with a random Number between 1 and 100 then check for every:
1 | if VAR < = 10 then -- Lower/Same = 1-10 = 10% |
2 | --script |
3 | end |
and then
1 | elseif VAR > 10 and VAR < = 25 then --Higher than 10 up to 25 (15%) |
2 | -- script |
3 | end |
Then you have 75% left. Let's just make one that will happen with a Chance of 75%:
1 | elseif VAR > 25 and VAR < = 100 then --Higher than 25, same or smaller than 0 (75%) |
2 | -- script |
3 | end |
There are some simpler ways, but this is good to start. Just to copy:
1 | if VAR < = 10 then -- Lower/Same = 1-10 = 10% |
2 | --script |
3 | end |
4 | elseif VAR > 10 and VAR < = 25 then --Higher than 10 up to 25 (15%) |
5 | -- script |
6 | end |
7 | elseif VAR > 25 and VAR < = 100 then --Higher than 25, same or smaller than 0 (75%) |
8 | -- script |
9 | end |
First, make two tables containing your values, One for percentages, and another for rewards.
example:
01 | local ChanceTable = { |
02 | [ "Common" ] = 95 , --Commas are used to separate a value from another |
03 | [ "Uncommon" ] = 85 , |
04 | [ "Rare" ] = 75 , |
05 | [ "Epic" ] = 25 , |
06 | [ "Legendary" ] = 10 , |
07 | } |
08 |
09 | local RewardTable = { |
10 | [ "Common" ] = 500 , |
11 | [ "Uncommon" ] = 1000 , |
12 | [ "Rare" ] = 2500 , |
13 | [ "Epic" ] = 6000 , |
14 | [ "Legendary" ] = 10000 , |
15 | } |
This will make it easier to add more rarities in the future.
Now, make a random number generator that goes from 0 to 100 like this:
1 | local RNG = math.random( 0 , 100 ) --this automatically gives a number from 0 to 100 |
Next, make a loop and a rarity variable. This will take your random number and scroll through the rarities, just make sure you have them in descending order (highest to lowest) in the table.
1 | local CurrentRarity = "None" --Have the variable in advance |
2 |
3 | for RarityName, RarityPercent in pairs (ChanceTable) do |
4 | if RNG < RarityPercent then |
5 | CurrentRarity = RarityName |
6 | end |
7 | end |
Every time the number is lower than the rarity the "CurrentRarity" variable changes to suit the one you passed.
After this, you can make another loop to reward the player:
1 | for RarityName, RarityReward in pairs (RewardTable) do |
2 | if CurrentRarity = = RarityName then |
3 | CurrencyValue.Value + = RarityReward --Change "CurrencyValue" to the stat you use |
4 | end |
5 | end |
It is also recommended to use a RemoteEvent if you're doing this on a LocalScript, since it won't save if you're doing this from there
Best way to do it is based on the value of a chance, make for loop of that chance and put it in a new table, once all the items are there pick a random item. This will work as a chance system