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
The problem seems to lie within the ModuleScript. It is returning nil
@Memotag I edited the post for the ModuleScript is there anything wrong with it?
Does my corrected code work?
Yes
Could you please mark my reply as the answer
Sorry How Do I Do That?