#percentage chance system

1 messages ยท Page 1 of 1 (latest)

trim meadow
#

Set values ("weight") for each rarity

#

So common = 6
legendary = 1

feral osprey
#

generate a seed

trim meadow
#

wait lemme see

feral osprey
#

and use math.random

trim meadow
#

guy above probably knows more than me so listen to him

feral osprey
#

assign a rarity to all things

#

and call on the rarity to decide the probability

#

Unless you want something more specific

zealous forum
ornate heron
#
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?

zealous forum
feral osprey
#

^

#

my cat is messing with my monitor 1 sec

ornate heron
#
math.randomseed(math.random(1,1000000))
#

?

feral osprey
#

This is not what you wanna do

#

This how I generate the seed

ornate heron
#

Fair enough, there won't ever be a duplicate seed that way

feral osprey
#

I'm not sure

#

its better than nothing ig

zealous forum
#

the math.round is useless

ornate heron
#

Uh

#

Fairly certain that os.time() is still smaller than math.huge, or even the 32bit int cap

#

Idk if those are the same

feral osprey
#

both of my cats are destroying everything rn ๐Ÿ˜ญ

zealous forum
#

already tried

feral osprey
#

just do os.time

zealous forum
#

yeah

feral osprey
#

and handle it on the server

#

impossible to predict prob

#

due to delay

ornate heron
#

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)
feral osprey
#

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

ornate heron
#

os.tiime() doesn't store ms though

feral osprey
#

You would use a remote to initiate the roll

#

Even if it's action based

#

there is still delay

ornate heron
#

Oh, I suppose, the delay to the server counts as something don't it

feral osprey
#

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

ornate heron
#

Returns the number of seconds since the beginning of the Unix Epoch

#

Jan 1 1970 00:00:00

#

In UTC time

feral osprey
#

yooo

#

it might be possible to predict it

ornate heron
#

I mean

feral osprey
#

if you know the server os.time()

#

due to 1 sec update rate

ornate heron
#

You could use the uh, UnixTimeStampMillis property on DateTime

feral osprey
#

we just add a random secret number to it

#

like a key

#

+126

ornate heron
#

Then you'd have to predict down to the millisecond

ornate heron
feral osprey
#

do

zealous forum
#

@feral osprey am i being dumb i dont know

feral osprey
#

dont use os.clock()

zealous forum
#

my brain isnt functioning i havent had enough caffeine

feral osprey
#

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

ornate heron
#

<= Common is ~75%

zealous forum
#

no like

#

the error

ornate heron
#

Oh

#

I'm blind lol

zealous forum
#

else expected

feral osprey
#

put an end

#

or something

ornate heron
#

Wack

feral osprey
#

beneath that

ornate heron
zealous forum
#

no ends needed on ternary operators (conditional operators)

ornate heron
#

Ternary is base 3, if you've got no else how is it a ternary operator?

zealous forum
#

okay thats the reason it needs the else

#

im slow

ornate heron
#

number is the "Random" number correct?

zealous forum
feral osprey
#

I don't think there is need to make it more random

#

I can write the code real quick

#

1 sec

zealous forum
#

im so stupid

ornate heron
#

You are?

#

I forget to require() all the time when working with ModuleScripts, I doubt you're dumber than I!

feral osprey
#

I'm not sure what I will write will corelate to actual % chance

zealous forum
#

give it a shot ๐Ÿ˜ญ

ornate heron
#
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

zealous forum
#

i dont understand why this doesnt work

#

holy shit my brain

feral osprey
#

the complicated part

#

is what you wanna do

#

do you want duplicate abilities

#

a pity system or something?

zealous forum
#

no; and i feel like the easy part is gonna be picking the ability

feral osprey
#

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

zealous forum
#

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

zealous forum
# zealous forum

i just dont understand why the number <= chance check isnt working

#

and its just returning common every time

ornate heron
zealous forum
ornate heron
#

Odd

zealous forum
#

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

feral osprey
#

im writing

#

it

#

I'm writing in vs code

#

because I wanna try something new and I'm not used to it

ornate heron
#

Got any idea?

zealous forum
#

send your second argument

feral osprey
#

do you want me to make it efficient or just make it work

ornate heron
#

That's not the error that math.huge returned either

#

I'll try a smaller number though

feral osprey
#

@zealous forum Do you want me to just return a random ability based on a number?

#

for example 15,25,30

ornate heron
#

Same error, and the line of code generating the error

feral osprey
#

do you want to store rarities?

zealous forum
ornate heron
#

Nope

zealous forum
ornate heron
#

That is the 32bit unisgned int limit

#

I could try halving it

feral osprey
#

Ok I'm just gonna make it simple and you can tweak it

ornate heron
#

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

zealous forum
ornate heron
#

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"

zealous forum
#

ill take a look when im back on pc give me a sec

ornate heron
#

Just a quick reply link to the original image

feral osprey
#

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
ornate heron
#
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?

feral osprey
#

I think i fixed it with the above

zealous forum
#

im trying to think how to return the ability wanted since its a dictioanry

feral osprey
#

try the code i sent

#

wait I can test it actually

ornate heron
zealous forum
ornate heron
#

Like HumanAbilities["Preservubg Spirit"]

zealous forum
#

the entire code

ornate heron
#

Oh

#

THanks I didn't see the expand arrow t-t

zealous forum
#

maybe instead of having the dictionary as the ability name make it apart of description? @ornate heron itd work

#

but just less readable

ornate heron
#

I'm working on the code you sent, I should have something done soon, I'll see if it works and send it over

feral osprey
#

#1137196694103134259 message this works now

zealous forum
feral osprey
#

in order for <= to work

#

we need to have 100

zealous forum
#

OHH

#

I SEE

#

yeah

ornate heron
feral osprey
#

in the previous code

#

what happens if I get 99

#

I get nothing

#

now worse case u get common

#

I'm tired af

ornate heron
#

I kinda see that now that I look at my code

feral osprey
#

and I did not realize at first

#

I should have seen it

ornate heron
#

Thanks for the enlightenment

zealous forum
#

rare doesnt show up

feral osprey
#

it does

#

just unlucky run?

zealous forum
#

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

feral osprey
#

debounce

#

should not affect it

zealous forum
#

yeah no

#

i was like

feral osprey
#

Imma head out

#

its super late

zealous forum
#

alright, goodnight

feral osprey
#

I would trust the code I wrote tbh

#

because I was tired when I wrote it

ornate heron
#

lol

feral osprey
#

but it seems to work or something

ornate heron
#

Gn

zealous forum
ornate heron
#

Uh

#

Not a clue t-t

feral osprey
#

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

ornate heron
#

Interesting

#

My friend would like that

zealous forum
#

my head

ornate heron
#

Just ignore

zealous forum
#

IT DIDNT EVEN WORK LMFOAFO FUCK

ornate heron
#

LOL

zealous forum
#

you can have innapropiate words in scripts?

feral osprey
#

yes

#

but it's not professional

zealous forum
#

no like

#

a long time ago i heard roblox checks scripts for

#

innapropiate words aswell

feral osprey
#

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

zealous forum
#

its due to the for loop

#

yeah i fixed it

ornate heron
#

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

zealous forum
ornate heron
#

Yep

zealous forum
#

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

ornate heron
#

It's really odd though, it should iterate three times and send the lowest value

ornate heron
zealous forum
#

100

#

rare is 25

#

legendary is 5

ornate heron
#

I am confusion right now

zealous forum
#

and i dont know; it just doesnt work without the breka

#

LMFAOFA

ornate heron
#

break doesn't need a semi colon either

zealous forum
#

i know

#

lua doesnt use semicolons for almost anything

ornate heron
#

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

zealous forum
#

@ornate heron

ornate heron
#

I know why.

#

It's because Lua is built different.

#

Built different in the most Lua way possible

zealous forum
#

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..? ๐Ÿ˜”

ornate heron
#

just finished I think

zealous forum
#

this wont return legendary..

#

LMFAOFO

#

FFS

ornate heron
#

it'll return if value is 5 < won't it?

zealous forum
#

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.

ornate heron
#
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

zealous forum
#

also whats maxn tavble function

#

too lazy to search up

ornate heron
#

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*

zealous forum
#

@ornate heron am i being dumb

#

it doesnt print the x

#

even though rarity == common

ornate heron
#

If it prints x

#

Then it's found a valid match to your thing

zealous forum
ornate heron
#
        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

zealous forum
#
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

ornate heron
#

Have you edited the module table?

#

Because I'm still on the one you send a while ago

zealous forum
ornate heron
#

Okay so you just moved what was module into Abilities which is inside module

#

kk

zealous forum
#

yeah, just incase i needed to access it from the outside itd be easier

ornate heron
#

But I'm going to head out for the night, hope I helped at least a little