How to fix class random tier RNG (reuloaded because code was changed a lot)

I have a script that give a random class tier for items to spawn. And for some reason even after running it 100s of times, it always give me common.

heres the code:

local class = script.Parent

class.Value = ""

local Found = false
while Found == false do
	print("retrying")
	local mathh = math.random(1, 35)

	if mathh > 1 and mathh < 10 then
		print("Got Common")
		class.Value = "Common"
		Found = true
	elseif mathh > 1 and mathh < 8 then
		print("Uncommon")
		class.Value = "Uncommon"
		Found = true
	elseif mathh > 1 and mathh < 6 then
		print("Got Rare")
		class.Value = "Rare"
		Found = true
	elseif mathh > 1 and mathh < 4 then
		print("Got superrare")
		class.Value = "SuperRare"
		Found = true
	elseif mathh > 1 and mathh < 2 then
		print("Got Legendary")
		class.Value = "Legendary"
		Found = true
	else
		class.Value = ""
	end
	task.wait()
end

Please help

Because in the if statement check first if the number is bigger than 1 and less than 10 so it will always print Commn because if you got a number like 7 it’s smaller than 10 so it will print Commn and will no move on for the next if statement

to fix it change from smaller than “<” to bigger than “>”

1 Like

and is just optional but if you want a system the actually uses rarity here a simple system:

local items = {
    NoobSword = 100 - (5 + 0.5), -- all items should = 100
    ProSword = 5,
    MagicSword = 0.5,
}

local total = 0
for index, chance in items do total += chance end 
if total ~= 100 then error("total is not 100%") end

local function Pick()
    local rand = math.random() * total
    for index, chance in items do
        rand -= chance
        if rand <= 0 then return index end
    end
end

local function PrintChances()
    for index, chance in items do
        print(index, math.round(chance / total * 100) .. "%")
    end
end

PrintChances()
print(Pick())
1 Like

so do i change all “<” to “>” and vice versa?

1 Like

i jsut tried that with another 100 spawn locations, and its still all common

Remove the first “mathh > 1” because mathh will awlays be 1 or bigger it can’t be smaller and change the others from < to > like this:

local class = script.Parent

class.Value = ""

local Found = false
while Found == false do
	print("retrying")
	local mathh = math.random(1, 35)

	if mathh > 10 then
		print("Got Common")
		class.Value = "Common"
		Found = true
	elseif mathh > 8 then
		print("Uncommon")
		class.Value = "Uncommon"
		Found = true
	elseif mathh > 6 then
		print("Got Rare")
		class.Value = "Rare"
		Found = true
	elseif mathh > 4 then
		print("Got superrare")
		class.Value = "SuperRare"
		Found = true
	elseif mathh > 2 then
		print("Got Legendary")
		class.Value = "Legendary"
		Found = true
	end
	task.wait()
end

i tested it again, still get common like 100% of the time

Actually no, now i am getting more classes, thanks mans!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.