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 make this rarity system support decimals?

Hello guys so basically i want this code to be able to support decimals how would i accomplish this here is the code below

local Items = {
	{"Pig",1},
	{"Dog",5},
	{"Cat",99},
}
local TotalWeight = 0

for _,ItemData in pairs(Items) do
	TotalWeight = TotalWeight + ItemData[2]
end

local function chooseRandomItem()
	local Chance = math.random(1,TotalWeight)
	local Counter = 0
	for _,ItemData in pairs(Items) do
		Counter = Counter + ItemData[2]
		if Chance <= Counter then
			return ItemData[1]
		end
	end
end

for i=1,100 do
	wait()
	print(chooseRandomItem())
end

Thanks for reading this, Hope you can help.

1 comment

0

What do you mean by "support decimals"?

1 answer

✓ Accepted answer

Explanation

For loops can be a little finicky some times. I'm not all that sure if they can support decimals or not but I have a good idea of how to get a very very similar effect.

The Solution

To start you have to think math for a second. Decimals such as 0.1 or 0.5 are just normal numbers divided by 10.

With this in mind you could have the same effect as decimals but making it multiplied by 10. Look at the pig value; if it was a decimal like 1.2 (or whatever you want), its the same thing as 12. When you make the number bigger it shouldn't affect the rarity because all the values would be bigger and it would just total up in the TotalWeight. If you goal was to make an item (eg. pig) be 0.1 rarity and everything else 25, then that's the same as the pig being 1 rarity and everything else bring 250.

I would provide a script, but it's more specific to what you want, so give it a shot with your newfound ideas.

If you have any other questions, just reply to this answer! :)

-Crystal

1 comment

0

I see so the higher rarity the other pets have the lower chance u will get the rarer pets if that makes sense thanks for the help.

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