in that game you can roll and it will give you a random item, items have probability like 1 in 2, 1 in 100 etc
now how can i achieve something like that? all i need is a function which will return a random item from a table [the higher probability = the harder to get]
I have my table here
local SwordModule = {
{model = nil, probability = 2},
{model = nil, probability = 2},
}
local function getRandomItem(t)
local totalProbability = 0
for _, item in ipairs(t) do
totalProbability += item.probability
end
local randomNumber = math.random(1, totalProbability)
local cumulativeProbability = 0
for _, item in ipairs(t) do
cumulativeProbability += item.probability
if randomNumber <= cumulativeProbability then
return item.model
end
end
end
print(getRandomItem(SwordModule)) --example: prints the item
If you wanna make a rolling system similar to sols rng, you’re gonna need to use percentages, also called weights. Im also pretty sure there are tutorials on youtube that go into depth on how to create a similar system.