Attempt to get length of a nil value

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Fix this error

  2. 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
  1. 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

2 Likes

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.

They are both nil. Though i have no clue why as its a math.Random

my bad, i didnt read the code through, i was wrong.

rolling_module.chooseAbility = function(rarity)
	local abilitys = rolling_module.abilitys[rarity]
	
	print(abilitys)
	local ability = abilitys[math.random(1, #abilitys)]
	return ability
end

what does print(rarity) output?

I’m pretty sure he meant it printed nil, hinting that the issue isn’t int the module but in the script where the module is being called?!

1 Like
	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)

its inside a local script

These don’t add up to 1,000, they should since you’re doing math.random(1, 1000)

You should change this to

-- @ 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:

  1. Make sure your weights add up to 1000 (or adjust the random number range)
  2. 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).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.