Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I go about getting a random value based on percentages?

I attempted to give it a try, but so far no luck. I was wondering if I could hop on scriptinghelpers and maybe get some help. Heres my script:

lastnamepercentages = {
	{Name = "Name1", Percentage = 0.5},
	{Name = "Name2", Percentage = 0.5},
	{Name = "Name3", Percentage = 0.3},
	{Name = "Name4", Percentage = 0.3},
	{Name = "Name5", Percentage = 0.2},
}

function lastnamepercentages:RandomObject()
	local randomize = math.random();
	local pastPercentage = 0;
	for i = 1, #self do
		if randomize < self[i].Percentage + pastPercentage then
			return self[i];
		end
		pastPercentage = pastPercentage + self[i].Percentage;
	end
end

plr.PlayerAdded:Connect(function(plr)
	local randomClan = lastnamepercentages:RandomObject()
	print(randomClan)
end)

1 comment

0

I don't understand what this is supposed to do, sorry if it's an inconvenience, but do you mind elaborating more?

1 answer

✓ Accepted answer

The easiest way is to treat the percentage as a whole number, you also have 180% there, you need to divide it between 100%

You would assign each value a number for example

Common = 50 
Rare = 30 
Very rare = 15 
Legendary = 5 

And you would get a math.random number from 0-100

If its 0-5 you get the legendary option, 5-15 you get the very rare, 15-30 you get the rare, 30-50 you get the common

If you have multiple items with the same rarity, you just go and use math.random again but just between all the common items for example.

This discussion is preserved as it was. New answers, comments, and votes are closed.