Attempt to index nil with "Name"

This appeared in my output:CaptureFORUM7

I need help to fix the error.

Here is the script:

local cost = 800

local petModule = require(game.ServerScriptService:WaitForChild("PetModule")) 

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	
	if player.leaderstats.BugCoins.Value >= cost then
		
		player.leaderstats.BugCoins.Value = player.leaderstats.BugCoins.Value - cost
		
		local pet = petModule.chooseRandomPet()
		
		print(pet.Name.." selected")
	
		game.ReplicatedStorage.HatchEgg:FireClient(player,pet)
		
	end
	
end)

Hello. Your module is returning nil try check a module or show us script in module.

hello its me again. Like @EsplishData said, if its returning nil, its probably an issue with your module.

here is my module script

local petModule = {}

petModule.pets = {

	["UnCommon"] = {
		game.ReplicatedStorage.Pets.BlackCaterpillar;
		game.ReplicatedStorage.Pets.WhiteCaterpillar;
	};
	
	["Common"] = {
		game.ReplicatedStorage.Pets.LightGreenCaterpillar;
		game.ReplicatedStorage.Pets.GreenCaterpillar;
		game.ReplicatedStorage.Pets.DarkGreenCaterpillar;
	};

}

petModule.rarities = {
		
	["UnCommon"] = 24;
	
	["Common"] = 55;
	
}

petModule.chooseRandomPet = function()
	
	local randomNumber = math.random(1,100)
	
	local counter = 0
	
	for rarity, weight in pairs(petModule.rarities) do
		counter = counter + weight
		if randomNumber <= counter then
			
			local rarityTable = petModule.pets[rarity]
			local chosenPet = rarityTable[math.random(1,#rarityTable)]
			
			return chosenPet
			
		end
	end
	
end

return petModule
1 Like

here is my module script!

local petModule = {}

petModule.pets = {

	["UnCommon"] = {
		game.ReplicatedStorage.Pets.BlackCaterpillar;
		game.ReplicatedStorage.Pets.WhiteCaterpillar;
	};
	
	["Common"] = {
		game.ReplicatedStorage.Pets.LightGreenCaterpillar;
		game.ReplicatedStorage.Pets.GreenCaterpillar;
		game.ReplicatedStorage.Pets.DarkGreenCaterpillar;
	};

}

petModule.rarities = {
		
	["UnCommon"] = 24;
	
	["Common"] = 55;
	
}

petModule.chooseRandomPet = function()
	
	local randomNumber = math.random(1,100)
	
	local counter = 0
	
	for rarity, weight in pairs(petModule.rarities) do
		counter = counter + weight
		if randomNumber <= counter then
			
			local rarityTable = petModule.pets[rarity]
			local chosenPet = rarityTable[math.random(1,#rarityTable)]
			
			return chosenPet
			
		end
	end
	
end

return petModule
1 Like

Weird. Maybe the chosen pet is nil so that you returning nil

1 Like

I think there is an issue because your math.random(1,100) is greater than the sum of you rarities in the table. You table has a total weight of 79, but you are trying to loop through your table which doesn’t even add to 100. What you’re trying to do is check for a random number between 1 100, and if <= then it selects that., but because your rarities don’t add to 100, if the number is greater than that, there would be an error. I might be wrong but consider changing it match to your weighted rarities. Otherwise, it could a problem with your chosen pet.

3 Likes