Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Erroring to choose a random pet, anyone can tell me whats the problem and how to fix?

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

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

I am a bit confused with this error. I had used a click detector, got it to work. But it's suppose to transition to something . But instead gives out this error

Workspace.Model.text.ClickDetector.Script:9: attempt to index nil with 'chooseRandomPet'

help

local petModule = {}

petModule.Pets = {
	
	["Legendary"] = {
		game.ReplicatedStorage.Pets.Burger;	
	};
	
	["Rare"] = {
		game.ReplicatedStorage.Pets.Jellyfish;
	};
	
	["Common"] = {
		game.ReplicatedStorage.Pets.LightDemon;
	};
	
	
}
-- 100 Total Weight
petModule.rarities = {
	
	["Legendary"] = 10;
	
	["Rare"] = 30;
	
	["Common"] = 60;
	
	
}

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

6 comments

0

The problem seems to lie within the ModuleScript. It is returning nil

0

@Memotag I edited the post for the ModuleScript is there anything wrong with it?

0

Does my corrected code work?

0

Yes

0

Could you please mark my reply as the answer

0

Sorry How Do I Do That?

2 answers

I think the problem here is that you are assigning the function to a variable, so instead of doing:

local pet = petModule.chooseRandomPet()

Just do:

petModule.chooseRandomPet()

Then of course if you want to see what pet was selected you could check through the module script. Hope this works

1 comment

0

Assigning a function to a variable via a ModuleScript is not an issue.

Please read through your script carefully. Scripts are case sensitive.

Corrected code:

local petModule = {}

petModule.Pets = {

    ["Legendary"] = {
        game.ReplicatedStorage.Pets.Burger; 
    };

    ["Rare"] = {
        game.ReplicatedStorage.Pets.Jellyfish;
    };

    ["Common"] = {
        game.ReplicatedStorage.Pets.LightDemon;
    };


}
-- 100 Total Weight
petModule.rarities = {

    ["Legendary"] = 10;

    ["Rare"] = 30;

    ["Common"] = 60;


}

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 comment

0

Ah well. Thank you very much.

This discussion is preserved as it was. New answers, comments, and votes are closed.