How to make a chance script

I want to make a chance script that allows a player to get a random item and the chances can be 50, 90, 8.5, 0.1,0.01

My old script that doesn’t work properly

How can I I achieve this?

1 Like

I use this function for my probabilities. Let me know if you have any questions

--Table of items and their probabilities
local itemProbabilities = {
    firstItem = 50,
    secondItem = 10,
    thirdItem = 20,
    fourthItem = 20,
}

--randomItem function, which passes through the table as a parameter
local function randomItem(items)
    local randomNumber = math.random(1, 100) --Generating random number from 1-100
    local correspondingProbability = 0
    
    --What this does is it adds up all the probabilities, then when it finds the range in between the probability in the table,
    --it returns the corresponding key.
    for key, probability in pairs(items) do
        local previousProbability = correspondingProbability
        correspondingProbability += probability

        if previousProbability < randomNumber and randomNumber <= correspondingProbability then
            return key
        end
    end

end

local choosenValue = randomItem(itemProbabilities)
print(choosenValue) -- prints your key value from the table
2 Likes

How would I add probabilities that are like 0.1, 0.01

Scale it from 100 to 1000 for 0.1 and then 10000 for 0.01

What do you mean so instead of 1,100 it’s 1,1000

Make a table of numbers and divide 100 by the total count.

2 Likes

It’d be better to use Random as that gives you random decimal numbers as opposed to math.random that will return an integer.

local rng = Random.new() -- Place this outside the function near top of script.

-- Inside function.
local randomNumber = rng:NextNumber(0, 100) -- 0 to 100 including decimals.
2 Likes

Would this include 0.1,0.01? When returning the #

yes

How would a number like 38 be returned as

Do u use math.random or random:nextnumber abd which is better and why

math.random returns integers, so math.random(2, 3) could return 2 or 3, but not 2.5
Random:NextNumber includes decimals, so Random:NextNumber(2, 3) could return 2, 3, or 2.5

For your case I think you want to use Random:NextNumber

1 Like

Just use table.insert and make it to where if the number that is coordinated to lets say speedbost is chosen it would give the player that so for example if the math.random() choses 54 and thats speedboost then yea it would chose speed boost

Wdym can you please show me an example

Ig you could do that, or this way:

local Chance = math.random(0,100) -- increase 100 for a higher possibility

if Chance == 100 then -- any number has a 1% chance of getting it
--player gets item
end
if Chance >= 0 and Chance <= 50 then -- about a 50% chance of getting something
--player gets item
end
1 Like

But this has no 0.1,0.01 chances in the script

local math.random.(0,1000)

1 number within that^^^ code is equivalent to 0.1%, add another 0 to 1000 and it becomes 0.01. That’s what I briefly stated in the post above.

But I want a rng variable that generates a number that can be a whole number or a 0.1,0.01 decimal

-- the items we are going to give
local items = {
    game.ServerStorage.Item1,
    game.ServerStorage.Item2,
    game.ServerStorage.Item3,
    game.ServerStorage.Item4,
}

-- the chances of each item
local chances = {
    9000,
    5000,
    850,
    10,
    1,
}

-- lets add all the chances together in this case it will be 14861
local total = 0
for i, chance in ipairs(chances) do
    total += chance
end

-- this function will get a random item with the chances we set
local function SelectRandom()
    local random = math.random(total) -- pick a random number from 1 to 4861
    for i, chance in ipairs(chances) do -- loop each chance
        random -= chance -- subtract the chance from the random number
        if random > 0 then continue end -- if the random number is still greater then 0 then skip this item
        return items[i] -- if random is less or equal to 0 then return this item as the selected
    end
end

-- lets print some random items to see what we get
print(SelectRandom())
print(SelectRandom())
print(SelectRandom())
print(SelectRandom())
print(SelectRandom())

if we multiply your numbers 90, 50, 8.5, 0.1, 0.01 by 100 we can convert them to hole numbers
and we do this becaise math.random() only returns hole numbers we could use Random:NextNumber(min, max) but that would require more code so to keep it simple i just multiplied your numbers by 100


total = 9000 + 5000 + 850 + 10 + 1 = 14861

  1. 9000 in 14861 chance 9000/14861*100 = 60.56120045757351%
  2. 5000 in 14861 chance 5000/14861*100 = 33.64511136531862%
  3. 850 in 14861 chance 850/14861*100 = 5.719668932104165%
  4. 10 in 14861 chance 10/14861*100 = 0.0672902227306372%
  5. 1 in 14861 chance 1/14861*100 = 0.0067290222730637%
1 Like

With this info how would I make a luck gamepass? Also if I did math.random(1,10000)

And made an it statement for 10000 would that be 1 in 10k or more attempts to get something