I have a system in my game where if you survive something you get points, but what i want to do is make it so that it rewards different amount of points by choosing between numbers, how would i do that?
math.random.
Or, if you don’t want it to be limited to integers, create a Random.new
object then use the :NextNumber()
function on it.
but no something like 20-40 because math.random could choose numbers like 438243473284872348327
You can just use math.random(20,40)?
ok so how would i make it so it only chooses 20, 30 or 40 and nothing else
like not 34 or 26 because it would make the shop system weird in my game
possible = {20, 30, 40}
function RandPoints()
return possible[math.random(1, #possible)]
end
math.random(2,4)*10 should get you where you need
See math.random at the Developer Hub
-
When math.random is called without arguments, returns a real number in the range [0,1).
-
When called with an integer number m, math.random returns a integer in the range [1, m]
-
When called with two integer numbers m and n, math.random returns a integer in the range [m, n].
As JarodOfOrbiter said you can use math.random(2,4)*10 as that would get 20 or 30 or 40.
You can also make tables and find a random value in that table!
TheTable = {20, 30, 40, 50, 600, 7000} -- Put how much you want
local RandomAtTable = TheTable[math.random(#TheTable)] -- Gets random value in table