Attempt to index nil with "MaxQuantity" error

Hey guys! I hope you all do very very well! Being direct,
Forummer helped me to make a limiter on a “Role Giver”. But when the moment of giving everyone their role comes, this error comes in:

image

All of this happens on a moduleScript (that after it’ll be called in a ServerScript to give everyone their roles)

I don’t really know what can be causing the error here, I think it’s the way I’m trying to access the “MaxQuantity” value, so If you can give me a hand with this, even if It’s the smallest one, I’ll appreciate a lot your help and you!

Here’s the code:

  • ModuleScript (CardModule):

CardModule.MaxLimitOfRoles = {

["Vampire"] = {
	["MaxQuantity"] = 4;
};

["Villager"] = {
	["MaxQuantity"] = 1;
};

["Troll"] = {
	["MaxQuantity"] = 2;
};

}

local VampireCards = 0
local VillagerCards = 0
local TrollCards = 0


CardModule.ChooseRandomCard = function()
	local randomNumber = math.random(1,100)
	local counter = 0
	for rarity, weight in pairs(CardModule.Rarities) do
		counter += weight
		if randomNumber <= counter then

			local rarityTable = CardModule.Cards[rarity]
			local chosenCard = rarityTable[math.random(1,#rarityTable)]
			if chosenCard == "Vampire" then
				
				VampireCards += 1
				
			elseif chosenCard == "Villager" then
				
				VillagerCards += 1
				
			elseif chosenCard == "Troll" then
				
				TrollCards += 1
				
			end	
				
				if VampireCards > CardModule.MaxLimitOfRoles.Vampire.MaxQuantity or VillagerCards > CardModule.MaxLimitOfRoles.Villager.MaxQuantity or TrollCards > CardModule.MaxLimitOfRoles.Troll.MaxQuantity  then
				CardModule.ChooseRandomCard() --recursive (calls function again)
			else
				return chosenCard
			end
		end
	end
end

I have tried this to fix the error but it still doesn’t works:

VampireCards > CardModule.MaxLimitOfRoles["Vampire"]["MaxQuantity]
VampireCards > CardModule.MaxLimitOfRoles["Vampire"].MaxQuantity

The only one that seems to work is this:

if VampireCards >= 1 or TrollCards >= 1 or VillagerCards >= 1 then
CardModule.ChooseRandomCard()
else
   return chosenCard
end

The problem is that I actually tried and it gave 2 players the same role when the limit is 1.

But it gave me the error, please do whatever you can do, even the smallest thing, I’ll be very very VERY grateful, thanks! have a nice day! :wave: :smiley: :wink:

2 Likes

Never use module scripts, they are a problem for many scripters
Module scripts return exactly one value, but the tool and the table have several values

2 Likes

It returns a single role(Rarity system) but if that role has already been chosen for other players (and have passed the limit ) then it regives you a new Role that hasn’t passed the limit. The problem is on the limiter.

1 Like

I’ve replicated the module as best I can and the condition works fine as you have it.

The way you have defined MaxLimitOfRoles it is exposed outside the module. Is it possible you are modifying this table outside the module?

If you never use MaxLimitOfRoles outside the module then I would recommend changing it to a local variable, which would also help shorten the code a bit.

I have to disagree with @Woodx, modules are a great way to create re-usable code.

2 Likes

I also tried this:


CardModule.ChooseRandomCard = function()
	local randomNumber = math.random(1,100)
	local counter = 0
	for rarity, weight in pairs(CardModule.Rarities) do
		counter += weight
		if randomNumber <= counter then

			local rarityTable = CardModule.Cards[rarity]
			local chosenCard = rarityTable[math.random(1,#rarityTable)]
			if chosenCard == "Vampire" then
				
				VampireCards += 1
				
			elseif chosenCard == "Villager" then
				
				VillagerCards += 1
				
			elseif chosenCard == "Troll" then
				
				TrollCards += 1

			else	
				return chosenCard
			end
		end
	end
end

It doesn’t give me an error but It doesn’t work (so the limiter doesn’t work) because I’ve seen 2 players with the same roll (Troll) for example.

1 Like

This is the ServerScript that gives everyone their role (ability) and I suppose it’s the only thing that could be causing the problem or modifying it:

game.ReplicatedStorage:WaitForChild('RemoteEvents').SayRolesToEveryone.OnServerEvent:Connect(function(player)
	
	local GivingCards = game.ReplicatedStorage:WaitForChild('GeneralStuff').GivingCards
	
	local CardModule = require(game:WaitForChild('ServerScriptService').CardModule) --Rarities,etc.

	if GivingCards.Value == true and GivingCards.Value ~= false then

		wait(1)

		if GivingCards.Value == true then
			
			wait(3)
			
			if not player.Character:FindFirstChild('IsAWerewolf') then
			
				local ChosenCard = CardModule.ChooseRandomCard()
				local ChosenCardName = ChosenCard.Name
				
				print(ChosenCardName.." Card has been selected for the player "..player.Name)
				
				local LuckyVillagerCards = 0
				local VillagerCards = 0
				local TrollCards = 0
				
				if ChosenCardName == "Villager" then
					
					if VillagerCards == 0 then
						VillagerCards += 1
						local ClonedVillagerCard = game.ReplicatedStorage:WaitForChild('NightRoles').Villager:Clone()
						ClonedVillagerCard.Parent = player.Character
						ClonedVillagerCard.Value = true
						
					elseif VillagerCards >= 1  then
						ChosenCard = CardModule.ChooseRandomCard()
						local NewChosenCardName =  ChosenCard.Name
						print(NewChosenCardName.." rechoosed for the player: "..player.Name)
					 end
					
					
				elseif ChosenCardName == "Troll" then
					
					if TrollCards == 0  or TrollCards == 1 then
						TrollCards += 1 
						local ClonedTrollCard = game.ReplicatedStorage:WaitForChild('NightRoles').Troll:Clone()
						ClonedTrollCard.Parent = player.Character
						ClonedTrollCard.Value = true
						
					elseif TrollCards >= 1 then
						ChosenCard = CardModule.ChooseRandomCard()	
						local NewChosenCardName =  ChosenCard.Name
						print(NewChosenCardName.." rechoosed for the player: "..player.Name)
					end
					
					
				elseif ChosenCardName == "LittleGirl" then
					
					
					if LittleGirlCards == 0 then
						LittleGirlCards += 1
					local ClonedLittleGirlCard = game.ReplicatedStorage:WaitForChild('NightRoles').LittleGirl:Clone()
					ClonedLittleGirlCard.Parent = player.Character
					ClonedLittleGirlCard.Value = true
						
					elseif LittleGirlCards >= 1 then
						ChosenCard = CardModule.ChooseRandomCard()		
						local NewChosenCardName =  ChosenCard.Name
						print(NewChosenCardName.." rechoosed for the player: "..player.Name)
					end

			elseif player.Character.IsAWerewolf then
				print(player.Name..' is already a default werewolf')
			end
			
		end

	end
	
	
	
end)
1 Like

This is very misinformed. Most major projects and advanced developers use ModuleScripts and encourage it as well. Single-script architecture are an industry standard (other game engines force you to work this way) and they help your code be predictable, reusable and organised.

Use of ModuleScripts don’t have anything to do with OP’s problem. The error in the title states that they’re trying to access MaxQuantity on something that’s nil.

6 Likes

Very strange, can’t see anything in the server script that is changing the table so don’t understand why it doesn’t work for you.

If you could provide the full card module script I will look into further. I had to edit the script you posted because of references to parts that weren’t included

It can’t be a problem of client and server? I’ll explain myself:

So the Server changes a BoolValue on the ReplicatedStorage and a LocalScript on the Player fires the RemoteEvent (the ServerScript I gave you) and every player fires the RemoteEvent, maybe this can be the problem?

  • Here’s the LocalScript:
game.ReplicatedStorage:WaitForChild('GeneralStuff').GivingCards.Changed:Connect(function()

	if game.ReplicatedStorage.GeneralStuff.GivingCards.Value == true and game.ReplicatedStorage.GeneralStuff.GivingCards ~= false then
	wait(1)
		
		if game.ReplicatedStorage.GeneralStuff.GivingCards.Value == true then
			local player = game.Players.LocalPlayer

			game.ReplicatedStorage:WaitForChild('RemoteEvents').SayRolesToEveryone:FireServer(player)
				
		elseif game.ReplicatedStorage.GeneralStuff.GivingCards.Value == false or game.ReplicatedStorage.GeneralStuff.GivingCards.Value ~= true then
		print('GivingCardsValue == false')
		end
		
	end
	
end)

So if there is 12 players, the RemoteEvent “SayRolesToEveryone” will fire 12x times

You are firing the Module, from the script, it does not have a shared table,and I recommend Module scripters for making Core Gui, only its not misinformed

Module scripts are used mostly by Top Developers who make their own modules, and not many on DevForum are top developers, everyone is still learning, so I told him to try to use normal script next time
You might be a Top developer, thats why you are recommending Module scripts, but that is way too hard for normal scripters, and posting module scripts on DevForum, usually don’t get a solution, 75% of them are answered wrong, and maybe some of them are totally wrong or relatable

The thing is not about Module scripts, its about making a working thing, no one checks if you used Module scripts or normal scripts, but what they need is working stuff

More than half of code-based resources are ModuleScripts, it’s just easier to let developers integrate them into their own work instead of having the resource coerce a certain structuring requirement. Developers don’t just make them for their own experiences, they share code for others as well.

I’m not a top developer but I’m just saying that they really aren’t as complicated as you make them out to be. Poor personal experiences with ModuleScripts don’t make them inherently bad; they are very powerful development tools. You can learn ModuleScripts too as a “learning developer”.

You can reach out to me if you want to talk more about modules themselves. We should be more focused on the actual problem which is the index error.


Regarding the OP: I’m afraid we don’t have sufficient context to pluck out an error here. A lot of relevant code and setup is missing and some of the code isn’t formatted correctly so it’s difficult to copy and paste the code. Are you instead able to post a barebones repro file of the problem? I myself can’t make a repro because of said missing information. cc @TheGamerRoblox456

1 Like