#percentage chance system
1 messages ยท Page 1 of 1 (latest)
generate a seed
wait lemme see
and use math.random
guy above probably knows more than me so listen to him
assign a rarity to all things
and call on the rarity to decide the probability
Unless you want something more specific
imma make a system really quick and tell me if its what you mean; give me like 10 mins
local dice = math.random(1,100)
local common = 75
local uncomon = 20
if dice <= common then
-- Get Common thingy
elseif dice <= common+uncommon then
-- Get Uncommon thingy
else
-- Get Rare thingy
end
Would this not work?
it would but random isnt really random
Fair enough, there won't ever be a duplicate seed that way
look at the mess i did.
math.randomseed(math.round(math.random(0, os.time())))
the math.round is useless
Uh
Fairly certain that os.time() is still smaller than math.huge, or even the 32bit int cap
Idk if those are the same
both of my cats are destroying everything rn ๐ญ
put math.huge it'll error
already tried
just do os.time
yeah
Yeah, os.time() won't be bigger than the int cap for another 70 years
On the other and, this is really overkill
math.pow(2,128)
I was thinking about if an exploiter could predict the os.time based on the server location
but due to ms delay prob not
I might be talking nonsense rn
os.tiime() doesn't store ms though
You would use a remote to initiate the roll
Even if it's action based
there is still delay
Oh, I suppose, the delay to the server counts as something don't it
I don't even remember how os.time works
it just returns the time from the location of the server or something
it does not matter really
Returns the number of seconds since the beginning of the Unix Epoch
Jan 1 1970 00:00:00
In UTC time
I mean
You could use the uh, UnixTimeStampMillis property on DateTime
Then you'd have to predict down to the millisecond
math.round(os.clock())
?
do
@feral osprey am i being dumb i dont know
dont use os.clock()
my brain isnt functioning i havent had enough caffeine
SAME
it has perfomance impact If I remember correctly
from some benchmark
just do os.time() + 18
a random 2 digit number you can come up with
= would return Common ~25% of the time
<= Common is ~75%
else expected
Wack
beneath that
It's probably expecting an end before the return statement, at least I think that's what Yiannis is saying
no ends needed on ternary operators (conditional operators)
Ternary is base 3, if you've got no else how is it a ternary operator?
number is the "Random" number correct?
I don't think there is need to make it more random
I can write the code real quick
1 sec
im so stupid
You are?
I forget to require() all the time when working with ModuleScripts, I doubt you're dumber than I!
I'm not sure what I will write will corelate to actual % chance
give it a shot ๐ญ
local returnedRarity = 0
for rarity,chance in pairs(Rarities) do
if number <= chance then
returnedRarity = rarity
end
if returnedRarity:IsA(string) then
return returnedRarity
end
end
Would this work?
This being inside a function of course
the complicated part
is what you wanna do
do you want duplicate abilities
a pity system or something?
no; and i feel like the easy part is gonna be picking the ability
yeah it is
so you dont want duplicates?
I can write that as well if you want but you will need to use the player data
nah, but i know how to make checks for that
i can just do a math.random for how many abilities are of said rarity and then use checks to see if its the same ability if so reroll until its not the same else give the ability
i just dont understand why the number <= chance check isnt working
and its just returning common every time
Have you tried a bigger number than 75?
thats what the else is for
Odd
FIXED IT
it was the first check
i dont need to check if number is <100 because the math.random wouldnt allow it to go above anyway
im writing
it
I'm writing in vs code
because I wanna try something new and I'm not used to it
Got any idea?
do you want me to make it efficient or just make it work
Nope
That's not the error that math.huge returned either
I'll try a smaller number though
@zealous forum Do you want me to just return a random ability based on a number?
for example 15,25,30
Same error, and the line of code generating the error
do you want to store rarities?
isnt that the 32bit interget limit shortened
Nope
im already doing that server side
Ok I'm just gonna make it simple and you can tweak it
Turns out because I was over the 32bit signed int limit
Odd
Know why there is one weird line doing that?
local Rarities = {
["Common"] = 75,
["Uncommon"] = 20,
["Rare"] = 5,
}
function RandomRarity()
math.randomseed(math.random(1,2000000000))
local number = math.random(1,100)
local returnedRarity = 0
for rarity,chance in pairs(Rarities) do
if number <= chance then
returnedRarity = rarity
end
if returnedRarity == rarity then
print(number)
return returnedRarity
end
end
end
for i=1,10 do
print(RandomRarity())
end
Code that generated the output
on mobile rn but if you do if number < chance itll never go over 100
I know it never goes over 100, because the math.random caps at 100, I'm checking for "Is less than or equal to chance"
ill take a look when im back on pc give me a sec
Just a quick reply link to the original image
there is a mistake
I figured it out lol
anything higher than 75 will not work
local SecretNumber = 21
local RNG = Random.new(os.time() + SecretNumber)
local Rarities = {
Epic = 15;
Legendary = 25;
Common = 100;
Rare = 80;
}
local Abilities = {
Epic = {"aaasd","adasdas"};
Legendary = {"asd1","sadsadf","aaasd22"};
Common = {"asdasd","aasdzz"};
Rare = {"aqsdasdd","|assadas"};
}
function RollAbility()
-- Roll Dice
local DiceResult = RNG:NextInteger(1,100)
-- Get Rarity
local ValidRars = {}
for _,Number in pairs(Rarities) do
if DiceResult <= Number then
ValidRars[#ValidRars + 1] = Number
end
end
local RarNumber = math.min(unpack(ValidRars))
local SelectedRarity
for Rarity,Number in pairs(Rarities) do
if Number == RarNumber then
SelectedRarity = Rarity
end
end
return Abilities[SelectedRarity][RNG:NextInteger(1,#Abilities[SelectedRarity])]
end
for i = 1,100 do
print(RollAbility())
end
function RandomRarity()
math.randomseed(math.random(1,2000000000))
local number = math.random(1,100)
local returnedRarity = 0
for rarity,chance in pairs(Rarities) do
if number <= chance then
returnedRarity = rarity
end
if returnedRarity == rarity then
break
end
end
local AbilitiesOfRarity = {}
for key,value in pairs(RacialAbilityTable) do
if value["Rarity"] == returnedRarity then
table.insert(AbilitiesOfRarity = {},RacialAbilityTable[key])
end
end
local randNum = math.random(1,#AbilitiesOfRarity)
return AbilitiesOfRarity[randNum]
end
Would this work?
I think i fixed it with the above
Fixed I think
im trying to think how to return the ability wanted since its a dictioanry
Return the ability's dictionary, rarity and all
Like HumanAbilities["Preservubg Spirit"]
the entire code
maybe instead of having the dictionary as the ability name make it apart of description? @ornate heron itd work
but just less readable
I'm working on the code you sent, I should have something done soon, I'll see if it works and send it over
#1137196694103134259 message this works now
how does the rarity system exactly work, i see you have 100 for common, 80 for rare, etc
????
in the previous code
what happens if I get 99
I get nothing
now worse case u get common
I'm tired af
I kinda see that now that I look at my code
Thanks for the enlightenment
noo, not yours i mean the one im making; ill try seeing whats the difference between mine and yours to check
if this works..
i dont see why it would
alright, goodnight
lol
but it seems to work or something
Gn
any idea on why? @ornate heron
I would throw it in the bin and look for a mathematically accurate solution
I can provide some leaked source code that was used to roll races for rouge lineage
before I head out
my head
Just ignore
IT DIDNT EVEN WORK LMFOAFO FUCK
LOL
you can have innapropiate words in scripts?
no like
a long time ago i heard roblox checks scripts for
innapropiate words aswell
the people who wrote this dont care tbh
deleted the code
due it being Leaked
its prob public
but I'm not sure if its good to post it here
Can you share your code?
local Rarities = {
["Common"] = 100,
["Uncommon"] = 25,
["Rare"] = 5,
}
function getRandomRarity()
math.randomseed(math.random(1,2000000000))
local number = math.random(1,100)
local returnedRarity = 0
for rarity,chance in pairs(Rarities) do
if number <= chance then
returnedRarity = rarity
print(number)
end
end
return returnedRarity
end
I'd like to fix this, but I don't know what's breaking
Wait
Wrong code mb
Now it's the right code
let me guess it just sends "common" no matter what
Yep
you had it almost right the frist time
function getRandomRarity(wantedRarity)
local returnedRarity;
if wantedRarity then -- check if you want to generate a random item out of "rarity"
return wantedRarity
end
math.randomseed(math.random(0, os.time())) -- makes it more "random" as it gives it different seeds each time
local number = math.random(0, 100) --
for rarity,chances in pairs(Rarities) do
if number <= chances then
--returnedRarity = if number <= chances then rarity else "Common a" -- if number <= 75 then return rarity else return a common
returnedRarity = rarity
break; -- breaks out of loop
end
end
print(number.. ": Number")
return returnedRarity.. ": Rarity"
end
this is how i have it
It's really odd though, it should iterate three times and send the lowest value
How in the world do you get values other than common though, what's your common value?
I am confusion right now
break doesn't need a semi colon either
I'm confused, so confused
I added the break
AND IT WORKS?????
I'm so confused
function getRandomRarity()
math.randomseed(math.random(1,2000000000))
local number = math.random(1,100)
local returnedRarity = 0
for rarity,chance in pairs(Rarities) do
if number <= chance then
returnedRarity = rarity
print(number)
break
end
end
return returnedRarity
end
This with the break works, without it though all it prints is Common
It's so weird
@ornate heron
I know why.
It's because Lua is built different.
Built different in the most Lua way possible
well all i know is it loops through the entirety of the rarities table and then does the only correct one then breaks @ornate heron
well
the most correct
@ornate heron wanna help me with the ability portion now..? ๐
just finished I think
wait
this wont return legendary..
LMFAOFO
FFS
it'll return if value is 5 < won't it?
LET ME CHECK AGAIN MAYBE IT WAS BC math.random WAS (0, 100) AND IT GAVE A 0
yeah
it was because of the 0
scared me.
function getRandomAbility(playerRace:string, randomRarity)
math.randomseed(math.random(1,2000000000))
local validAbilities = {}
local raceAbilityTable = module[playerRace]
if raceAbilityTable ~= nil then
for ability,description in pairs(raceAbilityTable) do
if description["Rarity"] == randomRarity then
table.insert(validAbilities,ability)
end
end
local number = math.random(1,table.maxn(validAbilities))
return validAbilities[number]
end
end
If you can fix this it should work
whats exactly wrong with it
also whats maxn tavble function
too lazy to search up
It turns the highest value key that's an integer
It returns the maximum number, maxn
But it returns the key with the highest value
But uh
math.random really hates it apparnetlyt
Apparently*
@ornate heron am i being dumb
it doesnt print the x
even though rarity == common
for ability,description in pairs(raceAbilityTable) do
if description["Rarity"] == randomRarity then
table.insert(validAbilities,ability)
end
end
That's what this is supposed to do
Sort of
function getRandomAbility(race, rarity)
local abilityTable = {}
for index,v in pairs(module.Abilities[race]) do
for ability, description in pairs(v) do
print("xx")
if rarity == description["Rarity"] then
print("x")
table.insert(abilityTable, ability)
math.randomseed(math.random(1, os.time()))
local number = math.random(1, #abilityTable)
--print(number, "ability number")
return ability[1]
end
end
end
end
heres the function
Have you edited the module table?
Because I'm still on the one you send a while ago
yeah, just incase i needed to access it from the outside itd be easier
I tried #validAbilities/#abilityTable, it returned this error
But I'm going to head out for the night, hope I helped at least a little
you did, thank you