How to make a chance script

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

while task.wait(1) do
	local t = {
		Bronze = {.1, 29.2};
		Rust = {29.3, 100}
	}
	local nt = {}
	for i,v in pairs(t) do
		for ii = v[1],v[2],.1 do
			table.insert(nt, i)
		end
	end
	local random = math.random(1,1000)
	local tierGained = nt[random]
	print(tierGained)
end

This looks like the 4th post you’ve made on this and there are solid answers in all of them. Forum Rules indicate that multiple posts of the same topic could be deleted. It might be good to change the topic to asking for help understanding the math behind the code or something to keep from running afoul of the moderators.

1 Like
local chance = math.random(1,1000)

if chance == 1000 then

      —- give something 

end

Is this script a 1 in 1,000?

Yes but it still calculates it as if it’s math.random(1, 100).

? Wdym by this it isn’t 1 in 1000