Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i make a random chance to do something efficently ?

i know how to do this

local num = math.Random(1,3)
if num == 3 then
end

but can i have something that does something like this

local chanceToDoThing = 10/100 --10%

and then it only does something 10% of the time

1 answer

✓ Accepted answer

You can just insert a value into a table 10 times and make one of those values a different value and then you can detect chance:

For example:

local rng = Random.new()

local tab= {'Rare', 'Common', 'Common', 'Common','Common', 'Common', 'Common','Common', 'Common', 'Common'}

local function returnChance()
	local chance = rng:NextInteger(1, #tab)
	return tab[chance]
end

print(returnChance())

3 comments

0

How does :NextInterger() work?

0

It's basically the same thing as math.random, it takes two parameters: first one is the minimum value and second one is the maximum value, if you do :NextInteger(1, 10) it will choose a number randomly from 1-10, that's what I did here, it chooses a number from minimum 1 to the maximum which is the length of the table then you index the number it returns.

0

Thanks! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.