You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Fix this error
What is the issue? Include screenshots / videos if possible!
12:42:20.125 nil - Client - rolling_module:38
12:42:20.125 ReplicatedStorage.folder_container.modules.rolling_module:39: attempt to get length of a nil value - Client - rolling_module:39
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
Yes and on youtube videos and googled it a ton, none helped
whole script
local rolling_module = {}
rolling_module.rarities = {
["Rare"] = 100, -- 100 = 10%
["Uncommon"] = 264, -- 264 = 26%
["Common"] = 550 -- 550 = 55%
}
rolling_module.abilitys = {
["Rare"] = {"Reckless"},
["Uncommon"] = {"Tremor"},
["Common"] = {"Prankster"} -- if you want two abilitys for one rarity do: {"Common1", "Common2"}
}
rolling_module.chooseRarity = function()
local randNum = math.random(1, 1000)
local counter = 0
for rarity, weight in rolling_module.rarities do
counter += weight
if randNum <= counter then
return rarity
end
end
end
rolling_module.chooseAbility = function(rarity)
local abilitys = rolling_module.abilitys[rarity]
print(abilitys)
local ability = abilitys[math.random(1, #abilitys)]
return ability
end
return rolling_module
This would mean that in the chooseAbility function, the ârarityâ parameter is not either âRareâ, âUncommonâ, or âCommonâ. Add a print statement about the given rarity to debug.
local module = require(game.ReplicatedStorage.folder_container.modules.rolling_module)
task.wait(Delay_Time)
wait(0.5)
local rarity = module.chooseRarity()
local ability = module.chooseAbility(rarity)
-- @ top of script
local total = 0
for key, value in rolling_module.rarities do
total += value
end
...
-- in rolling_module.chooseRarity
local randNum = math.random(1, total)
The percentages wonât matter anymore, but thatâs probably fine. You can normalize them later.
The issue is that your percentages donât add up to 100% (or 1000 in your case since youâre using math.random(1, 1000)). Currently, your weights add up to 914 (100 + 264 + 550), which means thereâs an 8.6% chance (1000 - 914 = 86) that the random number wonât match any of your rarities, causing the function to return a nil value as itâs not set to anything.
Here are the ways you could fix it:
Make sure your weights add up to 1000 (or adjust the random number range)
Add a fallback return value in case no rarity is selected (though this shouldnât happen if weights add up correctly)
Also, Iâd recommend using Random.new():NextInteger() instead of math.random() since itâs actually closer to true random (from what I heard it uses an external source to generate seeds for random generation).