Invalid Argument #1 to Insert (table expected got nil) and another Issue

So I had been debugging for just 5 Minutes and I can’t seem to fix it on my own, ill provide anything if possible!

Output

16:25:09.955 - ServerStorage.Aero.Services.GameStats.Stats:281: invalid argument #1 to 'insert' (table expected, got nil)
16:25:09.956 - Stack Begin
16:25:09.958 - Script 'ServerStorage.Aero.Services.GameStats.Stats', Line 281 - function PlayerRemoved
16:25:09.969 - Stack End

Code

-- for Inventory Saving or as Item Saving
        for Name, Items in pairs(Backpack:GetChildren()) do
            -- check if the ItemName was snot in the table before
            if PlayerData.Inventory[tostring(Items.Name)] then table.insert(PlayerData.Inventory, Items.Name) end

            for _, Objects in pairs(Items:GetChildren()) do
                if Objects:IsA("Folder") and Objects.Name == "Values" then
                    for ValueNames, Values in pairs(Objects:GetChildren()) do
                        table.insert(PlayerData.Inventory[tostring(Items.Name)], Values.Name)
                        print(ValueNames)
                        PlayerData.Inventory[tostring(Objects.Parent.Name)][Values.Name] = Values.Value
                        print("inserted")
                    end
                end
            end
        end

the only debugging, I actually did was access my old code but I can’t access it anymore and the other debugging was simply searching topics with the same problem as me but no one had a piece of code similar to mine in topics so that’s why I need help thanks!

1 Like

So table.insert() expects a table. You are providing an index of the table ‘Inventory’. If the value of the index is not a table, then the code will error. Make sure you are providing a table and not a table index whos value is not another table.

1 Like

but but how can I insert stuff to make it look like this?

PlayerData.Inventory = {
   ["WeaponName"] = {
       VALUE_NAME_HERE = Value
   }
}
1 Like

You dont need to use table.insert to do that. All you need to do is say:

PlayerData.Inventory["WeaponName"] = Values.Name

EDIT: or rather:

PlayerData.Inventory["WeaponName"][VALUE_NAME_HERE] = Values.Name
1 Like

its already done here

PlayerData.Inventory[tostring(Objects.Parent.Name)][Values.Name] = Values.Value

but it still keeps pushing errors and idk why

1 Like

Because you are still using table.insert, unless you arent anymore

1 Like

even tried removing it, I still got the errors

1 Like

What are your new errors? 303003030

1 Like

this code of default tools

Weapons = {
            ["Wooden Sword"] = {
                Path = game.ServerStorage.Items.Weapons["Wooden Sword"];
                Damage = 1;
                Type = "Sword";
                Rarity = "Common";
                ChanceToGet = 100;
                Physical = 1;
                Spell = 1;
                Upgrades = 0;
                MaxUpgrades = 15;
                Debounce = false;
                CanDamage = false;
                IsTradeable = false;
            }
        };
    };

error

17:10:11.573 - ServerStorage.Aero.Services.GameStats.Stats:282: attempt to index nil with 'ChanceToGet'
17:10:11.576 - Stack Begin
17:10:11.578 - Script 'ServerStorage.Aero.Services.GameStats.Stats', Line 282 - function PlayerRemoved
17:10:11.580 - Stack End
1 Like

Fixed it by removing it but I can just put tables into that table by just simply doing that in another module!

1 Like