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

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

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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())
0
How does :NextInterger() work? mattchew1010 396 — 6y
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. YabaDabaD0O 505 — 6y
0
Thanks! :) mattchew1010 396 — 6y
Ad

Answer this question