I want to take a random value like:
Common: 60%
Uncommon: 25%
Epic: 10%
Legendary: 5%
Then Print a Random Value
Like Case Opening System
Can Someone Help Me to Do It?
I want to take a random value like:
Common: 60%
Uncommon: 25%
Epic: 10%
Legendary: 5%
Then Print a Random Value
Like Case Opening System
Can Someone Help Me to Do It?
Instead of "math.random" i recommend you using Random because it supports decimal numbers instead of "math.random" which supports whole integers only.
The best way of generating random rarity is by using weight system, it's like you add chances of all rarities into one number, then generate random number in-between them, loop through every rarity chance and check if the chance is lower or is the random number:
-- 1
local Rarities = {
{"Legendary", 5},
{"Epic", 10},
{"Uncommon", 25},
{"Common", 60},
}
local function GetRandomRarity()
-- 2
local TotalValue = 0
for _, Rarity in ipairs(Rarities) do
TotalValue += Rarity[2]
end
-- 3
local RandomNumberGenerator = Random.new()
local Chance = RandomNumberGenerator:NextNumber(0, TotalValue)
local Value = 0
-- 4
for _, Rarity in ipairs(Rarities) do
Value += Rarity[2]
if Chance <= Value then
return Rarity[1]
end
end
-- 5
return nil
end
Here is explanation of each step.
Legendary = 5 to allow us to use "ipairs" to keep the order from lowest to highest, with keys you would have to use "next"/"pairs" loop which would not care about the order, bad. The first value in each table is name of the rarity and second one is the chance. Rarities must be sorted from rarest to most common to work.math.random(1, TotalValue) or RandomNumberGenerator:NextInteger(1, TotalValue) to unsupport decimal numbers.nil instead of void, you can change the return value for "Empty" or "None" too if you need, does not matter.i dont understand any of that but why not accept it
oh sorry i thought you know about loops, you should stick to wizard's answer then, his is simpler.
I'm not sure if you're overcomplicating your own thoughts, but this is very straightforward.
The four chances should add up to be 100, considering that the maximum percentage value is 1, and the minimum is 0. Percents work as x/100, with x being the number represented as the percent, like 60%, which is equivalent to 60/100, which is 0.6. That being said, your math.random() should actually look like this:
local value = math.random(1, 100) -- Start at 1 since it is the lowest non-zero chance possible. The range 1-100 is also 100 itself; 0-100 has a range of 101.
Now you should define your ranges. Going off your given percentages, your ranges are going to look like this:
There are two possible ways to set these ranges. The simplest way to do it is to check them directly:
if value >= 1 and value <= 60 then
-- Common
elseif value >= 61 and value <= 85 then
-- Uncommon
elseif value >= 86 and value <= 95 then
-- Epic
elseif value >= 96 then
-- Legendary
end
The other way, albeit more of a hassle, is to define NumberRanges. The difference here is, after creating NumberRanges using the NumberRange.new() constructor, you will replace the numbers in the above if statement(s) with NumberRange.Min and NumberRange.Max, respectively.
That is all you need to know about what you have asked.
OK, so I haven't been programming in Roblox Lua for a while now, so please do not expect it to work right away!
I would suggest giving 4 different variables different math.Random values and then printing it all together!
local commonValue -- Define values
local uncommonValue
local epicValue
local legendaryValue
game.Players.PlayerAdded:Connect(function() -- When a player is added, fire function
commonValue = math.random(1, 100) -- Set value to random integer between 1 to 100
uncommonValue = math.random(1, 75)
epicValue = math.random(1, 50)
legendaryValue = math.random(1, 25)
--If You want it to round up or down
commonValue = math.ceiling(commonValue) -- Round value up to nearest 10
uncommonValue = math.ceiling(uncommonValue)
epicValue = math.ceiling(epicValue)
legendaryValue = math.ceiling(legendaryValue)
--note: math.ceiling means round up and math.floor means round down
print(commonValue, uncommonValue, epicValue, legendaryValue)
end)
Hope it work and I hope it helps you out!
So its like 1, 100. The 100 is the chance?
100 is the most common and 1 is rare. 100 is max chance. You can change that to a lower number if you want!
WAIT, so it will print all common, uncommon, epic, and legendary value?
Yes
thats not what i need
Please explain more clearly then
@Lighting_Game27 LIKE adopt me egg system, like when i buy random sword. It will give me a random sword by a chance
You've said that three times now, if people still say "explain more clearly" then you have to understand it doesn't help to just repeat yourself. Explain. More. Clearly.
Ok, I will tell you right now that this will not work correctly. The random values should ALL be generated within the same function, not into 4 separate functions. This ensures weight, but dividing it evenly across 4 functions does not factor in weight.
This discussion is preserved as it was. New answers, comments, and votes are closed.
4 comments
Do you mean you want it to be increasing by 5 or any number between 1 to 100?
@Lightning_Game27 Like murder mystery opening case, egg on adopt me. Stuff like that.
@TheSaver31 could you please clarify EXACTLY what you need?
@Lighting_Game27 LIKE adopt me egg system, like when i buy random sword. It will give me a random sword by a chance