Help with making a Luck System

I have been working on a RNG game for about a Month now but there is one thing i tried to add but kept on failing

image

This is my Randomizer Script which i found Online i want to have Potions and Events in the Game where you get for example like 2X luck but i have no idea how to add a Luck System to this i tried looking online for solutions and found many Solutions but i have no idea how to add it if anyone is able to help me out on this it would be very appreciated

image

This is the Module Script where it gets the Gears from

2 Likes

Send the code into the forums, it will help more


image

Do this

local randomNumber = math.random(Sum) / 2

What it (should) do is make the math.random sum half, which results in rarer values

i tried it out and somehow if i roll with like / 1000000 it doesnt give me the rarest gears it only gives me Witches Brew which is 1 / 55 there are 2 more gears beyond that one is 1 / 60 the other 1 / 55m it should give that but somehow it wont

image

oh and here is the non image code:

function GetRandomItem()
local Sum = 0
for GearName,Chance in pairs(items) do
Sum += Chance
end
local randomNumber = math.random(Sum)
for GearName,Chance in pairs(items) do
if randomNumber <= Chance * globalluckmultiplier then
return GearName
else
randomNumber -= Chance * globalluckmultiplier
end
end
end

Make sure to put that code you type ``` before and after the code.

And for your code it could be a possibility that floating point numbers are affecting your randomization due to how small these decimal points are.

Also why not just use a bigger range like 1-100 for your random number and check the range the number is in, then apply it based off of that range?

Can you send both the codes (luck code and the gears code) and put them in code formatting so i can debug them easier?

like this

image

sure! i didnt know you can do that im not posting that much on the dev forum

local items = require(game.ReplicatedStorage.Gears)

function GetRandomItem()
	local Sum = 0
	for GearName,Chance in pairs(items) do
		Sum += Chance
	end
	local randomNumber = math.random(Sum)
	for GearName,Chance in pairs(items) do
		if randomNumber <= Chance  then
			return GearName
		else
			randomNumber -= Chance
		end
	end
end
local gears = {
	["Cheezburger"] = 50,
	["Pizza"] = 33.33333333333333,
	["Latke"] = 25,
	["Space Sandwich"] = 20,
	["Cake"] = 14.285714285714285,
	["Chocolate Milk"] = 12.5,
	["Turkey Leg"] = 10,
	["Waffle"] = 8.333333333333332,
	["Watermelon"] = 6.666666666666667,
	["Taco"] = 5,
	["Smore"] = 4.166666666666666,
	["Candy Bar"] = 4,
	["Tom's Beans"] = 3.571428571428571,
	["Foam Finger"] = 3.3333333333333335,
	["Pirate Juice"] = 2.857142857142857,
	["Bloxy Cola"] = 2.5,
	["Starblox Latte"] = 2.2222222222222223,
	["Witches Brew"] = 1.8181818181818181,
	["Bloxiade"] = 1.6666666666666667,
	["Illumina"] = 0.000001818181818181818,
}

return gears

1 Like

im sorry but i dont understand what you mean im not that good at scripting yet im horrible at making systems like these

math.random can output ONLY full numbers - 1, 2, 3, 4, …, 10000000. But not decimals. That’s the reason why Illumina will never be outputted (only possible situation if math.random will output epic combo after which illumina will be greated with matching combo of dictionary selections - in your case zero or 100, and ontop of that it needs to be selected as first/last dictionary value respectively)

What about luck - there’s 3 main ways to do it.

  1. Manually adjust odds for every additional stat possible.
  2. Use non-linear functions and adjust them:
local Sum = SumOfYourGears --(100 for example)
local ExtraLuck = Effect --(0.75 for example)
local Rng = math.random(Sum)^ExtraLuck --[==[ not true formula, only for
describing what I meant. Real one will be much more complicated.--]==]
  1. Use reroll system, where each 1 point of luck = one reroll. Rarest selected item is outputted. For decimal luck, make reroll having chance too - 0.5 luck = 50% of reroll.

round all numbers in the table to 2 decimal places, then change the code to make up for it. adding x100 should do it.

local items = require(game.ReplicatedStorage.Gears)

function GetRandomItem()
	local Sum = 0
	for GearName,Chance in pairs(items) do
		Sum += Chance*100
	end
	local randomNumber = math.random(Sum)
	for GearName,Chance in pairs(items) do
        local multchance = Chance*100
		if randomNumber <= multchance then
			return GearName
		else
			randomNumber -= multchance
		end
	end
end

you forgot to multiply by 100 there too.

my mistake, was in a rush to get the message out.

quick correction,

  • math.random(minNum, maxNum) will give an int between the minNum and maxNum
  • math.random(maxNum) will give an int between 0 and maxNum
  • math.random() will give a random float between 0 & 1

OP probably wants math.random()*Sum instead of math.random(Sum) and then everything else in the original code would work

1 Like

tried it out and it works perfectly! thank you very much!!!