How would I go about creating rng?

So basically, I’m trying to make a script where it would read chances for a certain item, create a random number and if the chances match up give the player that item, However I kinda realize that since I want to have multiple items have the same chance, and another thing is how would i really determine if the item is item 1 or item 2 if item 1 has a chance of 10%, item 2 has a chance of 5% but the random number is 7.5%, this probably isn’t the best way to go about things.

So does anybody know how I would go about creating rng?

1 Like

I have created something similar to this where items and chances are stored inside a script in a table. If you want, you may have a look at it to get an idea of what to do. My coding might be messy though. This code is meant for a tool, but you can repurpose it if you want.

local itemPool = { -- Name the string to your item name, and the number to the chance
	["SuperRareItem"] = 50, -- 1/50 or 2% chance
	["CommonItem"] = 2 -- 1/2 or 50% chance
} -- Items are held in ServerStorage under "Items"
local tool = script.Parent

tool.Activated:Connect(function()
	for item,value in pairs(itemPool) do
		local random = math.random(value)
		if random == value then
				print(tostring(random).." hit on "..item)
				local cloneItem = game:GetService("ServerStorage").Items:FindFirstChild(item):Clone()
				cloneItem.Parent = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent).Backpack
		else
			print(tostring(random).." did not hit "..item)
		end
	end
end

So I managed to make this code work, however one issue with it is that it is never 100% guaranteed that the player will get an item, which is kind of a problem.

local itemPool = { -- Name the string to your item name, and the number to the chance
	["SuperRareItem"] = 50, -- 1/50 or 2% chance
	["CommonItem"] = 1
} -- Items are held in ServerStorage under "Items"
local tool = script.Parent

tool.Activated:Connect(function()
	for item,value in pairs(itemPool) do
		local random = math.random(value)
		if random == value or value == 1 then
			print(tostring(random).." hit on "..item)
			local cloneItem = game:GetService("ServerStorage").Items:FindFirstChild(item):Clone()
			cloneItem.Parent = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent).Backpack
		else
			print(tostring(random).." did not hit "..item)
		end
	end
end)

Try this out.

So, another issue with this code would be if there were multiple common items one would always get prioritized over the other, however I can figure that one out on my own, I appreciate all of your help, thank you!