Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Line 31 Attempt to get length of a nil value?

Asked by 4 years ago

So I have a problem with my module script where is says there is a nil value I have tried going on the dev forum and looking it up, and just simply looking up the error but here is the script

P.S this is for a egg system

local petModule = {}

petModule.pets = {
    ["Epic"] = {game.ReplicatedStorage.pets.LavaBird};
    ["Rare"] = {game.ReplicatedStorage.pets.StereoPet};
    ["Uncommon"] = {game.ReplicatedStorage.pets.DodgeBall};
    ["Basic"] = {game.ReplicatedStorage.pets.BasicDoggo};

}

petModule.rarityTable = {

    ["Epic"] = 4;
    ["Rare"] = 16;
    ["Uncommon"] = 30;
    ["Common"] = 50;

}

petModule.chooseRandomPet = function()

    local randomNumber = math.random(1,100)

    local counter = 0

    for rarity, weight in pairs(petModule.rarityTable) 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

2 answers

Log in to vote
0
Answered by 4 years ago
local rarityTable = petModule.pets[rarity]
 local chosenPet = rarityTable[math.random(1, #rarityTable)]

I think the problem is that there is nothing called "Rarity" In Petmodule.pets, I might be wrong.

Ad
Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago

You have a naming mismatch. Your code will error on cases where the chosen Rarity is "Common", when in reality you're looking for "Basic." Try changing line 16's ["Common"] to line 07's "Basic", or vice versa.

Answer this question