Percentage randomizer picking only rare and uncommon every time

I made a percentage randomizer for a extremely basic case opening system but every time the function picks a random object, it always goes for 2 when it should be 1 and its always rare and uncommon. never common. This is the script:

function buy()
	
	local random_number = math.random(0, 100)
	
	if random_number <= 0 then --70%
		print("common")
	end
	if random_number < 70 then --25%
		print("uncommon")
	end
	if random_number < 95 then --5%
		print("rare")
	end
	
end
script.Parent.MouseButton1Click:Connect(buy)

as you can see, very basic and very annoying why it does not work. no errors are found in the output

If you know what’s wrong, please let me know.

1 Like

You are checking if random_number is less or equal to zero, which is a really low chance.
You would probably want to do something like this; Checking for greater than (>) rather than less than (<), and starting with the rarest and moving your way to the most common.

if random_number > 95 then --5%
	print("rare")
elseif random_number > 70 then --25%
	print("uncommon")
else
    print("common")
end
3 Likes

oh, my mistake that explains the only rare and uncommon. I forgot to change the symbols as this script was part of another I had in the game but I still have doubled selection

edit: I added the “elseif” and “else” and this stopped the multiple selection. thanks for the help!

2 Likes

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