Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to Make A Chance System?

Asked by 3 years ago

So here what i mean:

Rat - 30%
Dog - 25%
Cat - 25%
Golden Dog- 10%

How to make the system choose itself randomly with Chance based?

and Please explain to me and make it simple cause i only have 1 braincell left ;-;

Similar to:

Case Opening

Egg Hatching

Enemy Loot Drop

Thank's for Reading!

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Write table that use all items and percentage. Then we add all previous percentages to the total percentage. The total percentage is used in adding the number to the maximum chance of each item (Example: Dog have the max number of 31 due to addition of 30 from Rat), the random function will then random the number and get the percentage which will be used to compare the chance of each item.

This means that each item will be rarer depend on the number of items found in the table (Due to the random algorithm being nature of unlimited percentage). This algorithm also meant that percentage can be unlimited (technically there's a number limit such as 64 bits OR 32 bits but that's irrelevant) and never maxed out at 100%.

==== Random Number based on Algorithm ====

1st Rat: 1-30

2nd Dog: 31-55 (+30 from total number)

3rd Cat: 56-80 (+55 from total number)

4th Golden Dog: 81-90 (+80 from total number)

NOTE: ITEM IN TABLE MAY BE DIFFERENT DUE TO ORDERING!!

==== Code ====

The code will use at least 7 variables, 2 variables are a table to help in the for loop of the function.

local dropTable = {
    Rat = 30,
    Dog = 25,
    Cat = 25,
    Golden_Dog = 10
} -- Table which fit item shown in the question post

local actualTable = {} -- Table which list MAXIMUM chance

local totalPercent = 0 -- Add number to max chance
local altPercent = 0 -- Alternate version of totalPercent
local chance = 0 -- Chance randomized by math.random
local previousItem = 0 -- Chance of previous item to be compared

local drop = nil -- Item which is gotten from randomization

function addPercent()
    for i, v in pairs(dropTable) do
        wait()

        table.insert(actualTable, v + totalPercent)
        totalPercent = totalPercent + v
    end

    randomize()
end

function randomize()
    chance = math.random(1, actualTable[#actualTable])
    for i, v in ipairs(actualTable) do
        wait()

        if i > 1 then
            if chance < previousItem then
                break
            end
        end

        previousItem = v
    end
    translateItem(previousItem)
end

function translateItem(GivenChance)
    for i, v in pairs(dropTable) do
        wait()
        if GivenChance == v + altPercent then
            drop = tostring(i)
            break
        end
        altPercent = altPercent + v
    end

    print(drop) -- Let's pretend that this is the item you got from randomization (opening egg)

end

addPercent()

==== SIMPLIFIED EXPLANATION ====

addPercent() function

Step 1: Value from the initial table (dropTable) is added by the sum of the previous value (totalPercent) and insert into another table (actualTable). The number of each item is added in totalPercent for each loop.

randomize() function (activated after step 1)

Step 2: Random value is then created AFTER step 1 and saved in chance variable

Step 3: The first item (from actualTable) is always skipped and the number of the first items is saved as previousItem this is used to compare item which actually exists and avoid compare nil

Step 4: chance is then used in for loop of randomize() function. This will compare if chance is less than the value of the previousItem variable. If it failed then an item that has a higher value than chance variable will be saved as the previousItem variable.

Step 5: If it did found that chance is less than previousItem then the previousItem value is then used in translateItem() function as parameter.

translateItem() function (activated after step 4)

Step 6: The parameter is then used in comparing values from dropTable. Value from the initial table (dropTable) is added by the sum of the previous value like Step 1 but utilizes AltPercent instead of totalPercent. (This step DON'T insert value into table)

Final Step: If the parameter matches the value from dropTable plus AltPercent then the string (Golden Dog, Cat, Dog, Rat) will be saved in drop. Drop is then printed.

==== CONCLUSION ====

This looks like a request question to me but I want to share my code which helps in the randomization of the game. (Simulator Games is an example) You can say that the code above is now open-source. It's not that hard to write anyway because anyone can figure it out.

Happy Scripting.

0
If you found this solution (technically just answer to request) helpful either directly or indirectly then please accept this answer and help others who're looking for the same answer. Somone_exe 224 — 3y
0
Wow that's complicated. If you dont mind can you explain this a little bit more. Feel like im gonna need this later. iMazariuz 36 — 3y
0
I mean it's really complicated but all it did is using 3 for loop with each have different purpose. It's basically add sum of previous to the value (inserted in table) and then compare the random value, when the random value is less than value from the table it then use value from table AND translate into text. Somone_exe 224 — 3y
0
If the comment is complicated then you can see simplified explanation found in the post (I recently updated it) Somone_exe 224 — 3y
View all comments (5 more)
0
Hey, this is kinda unrelated but what does the hashtag represent in actualTable[#actualTable]. Sorry if this seemed dumb because i'm still fairly new to coding. iMazariuz 36 — 3y
0
#actualTable represent amount of item in table so actualTable has 4 items then #actualTable will become 4 Somone_exe 224 — 3y
0
so simply, drop on line 54 will result in a pet name? The_Saver31 260 — 3y
0
Yeah. You can change the line 54 to utilize dataStore service (save pet to dataStore) or show player the pet with GUI Somone_exe 224 — 3y
0
accept go brrrrrrrrrr The_Saver31 260 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I kinda have an idea about how you can do it, but I'm not so sure if this is the right way, or if this is the smart way. To be honest this seems so basic, and you might want to wait for a better solution.

Okay, so basically you gotta use math.random. But i think this will only work on single items. Like, for example if you open an box, you either get something, or nothing. So basically, you do math.random for example it's between 1-100 so the chance is one out of a hundred.

--This runs when it's called, maybe you're using a Remote Event or anything.
local chance = math.random(1,100)
if chance == 1 then --1/100
    print("Something1")
elseif chance >=2 and <=89 then --what is it, idk, 88/100 chance? Wow my math is bad
    print("Something3")
elseif chance >=90 then --10/100
    print("Something2")
end

This is like, the very basic way of doing it. If you're still new to coding and don't understand much.

0
Problem is you have to create EVERY if statements for every item. Sometime you may even have to add 200 if statements for 200 items. Somone_exe 224 — 3y
0
Well i figured so, that's why i said that this isn't a smart way. iMazariuz 36 — 3y
0
I found the solution (see my answer) to problem I mentioned earlier anyway. Somone_exe 224 — 3y

Answer this question