RNG Luck System

Good Morning! I have a problem with a Luck Rng system i made

So i made a working, good RNG luck system but i have no clue on how to implement luck on it

Please Help.

return {
	GetRandomIndex = function(Chances)
		for i, TableValue in ipairs(Chances) do
			local RolledNumber = math.random(1, TableValue[2])
			if RolledNumber == 2 and TableValue[2] > Chances[3][2] then
				return {
					TableValue[1];
					TableValue[2];
					TableValue[3]
				}
			end
		end
		
		return Chances[1]
	end,
}


6 Likes

You can use math.random. it generates random number. example: math.random(1,100) (its gonna generate a random in between 1 and 100) Just for the sake of it, copy and paste this script in studio. When you check the output you will see that it printed a random number.

local random = math.random(1,100)

print(random)

You can use Youtube tutorials for a deeper explanation, it’ll help you implement it to your system

1 Like

Im not sure if you understand. Im trying to make a higher chance for a higher number

3 Likes

depending on whether you use weights for chances, you can simply add values to the weights themselves rather than specifically modifying the random num. gen.

1 Like
local luck = 0
local max = 100 -- 1 in 100
print(math.random(1, max) <= 1 + luck) -- 1 + luck in 100 chance of printing true
1 Like

Please can you give me an example

1 Like

a bit complicated to show depending on the use case, but here’s a sum up;

local luckmodifier = 0; -- positive numbers = higher chance of getting lower-weight items
local totalweight = 0; -- internal rng stuff
local weights = {
    {
		baseweight = 1;
		item = 'rare item';
	};
	{
		baseweight = 5;
		item = 'uncommon item';
	};
	{
		baseweight = 10;
		item = 'common item';
	};
};

function getWeightedItem()
	totalweight = 0;
	for _, v in weights do
		v.weight = v.baseweight + luckmodifier;
		totalweight += v.weight;
	end;
	local rng = math.random(1, totalweight);
	local currentweight = 0;
	for _, v in weights do
		currentweight += v.weight;
		if rng <= currentweight then
			return v.item;
		end;
	end;
end

basically because you set everythings’ weight up, the lower-weighted items appear ‘more often’ as a direct result of luck. that’s more of a math thing, you can check out statistics and stuff.

edit to explain it ‘better’ in a way;
if u add +100 luck, rare will have a weight of 101 and common will have a weight of 110. yeah, statistically common will still show up a bit more often, but now the chances are almost the same with a +100 luck modifier

1 Like

I think this can help.

https://devforum.roblox.com/t/how-to-generate-a-random-number-with-a-higher-chance-to-have-a-low-value/1887013