How insert a table inside another table?

I made my datastore, that is my datastore table`

    ["Stats"] = {
	       ["Strength"] = {
		      ["Value"] = 0;
		      ["Multiplier"] = 0;
		      ["Boost"] = 0;
	       };
	       ["Endurance"] = {
		      ["Value"] = 0;
		      ["Multiplier"] = 0;
		      ["Boost"] = 0;
	       };
	       ["Mind"] = {
		      ["Value"] = 0;
		      ["Multiplier"] = 0;
		      ["Boost"] = 0;
	       };
           ["Jump"] = {
		      ["Value"] = 0;
		      ["Multiplier"] = 0;
		      ["Boost"] = 0;
           };	
           ["Speed"] = {
		      ["Value"] = 0;
		      ["Multiplier"] = 0;
		      ["Boost"] = 0;
		   }    
        };
        ["Leard"] = {
	          ["Reputation"] = 0;
	          ["Rank"] = "Innocent";
        };
        ["Missions"] = {
	    
        };
        ["Skills"] = {
	
        };
        ["Gold"] = 0

So i starded make the script to add skill for players, a skill have a lot of properties, so i need create a table per skill, so i made that method

    local ServerData = {}

local module = {}

 function module.New(p,data)
	ServerData[p.UserId] = data
	print("Hiii iam creating here")
 end

 function module.Get()
   return ServerData
 end

 function module.SetStats(p, Stat, I, Value)
	ServerData[p.UserId]["Stats"][""..Stat][""..I] = Value
 end

 function module.SetLeard(p, I, Value)
	ServerData[p.UserId]["Leard"][""..I] = Value
 end
 --Here that is the method
 function module.AddSkill(p, Name, Type, Element, Desc, Mana, Stamina, Level, MaxLevel, Upcost)
	print(Name..Type..Element..Desc..Mana..Stamina..Level..MaxLevel..Upcost)
	local TableD = {
	    [""..Name] = {
		  ["Type"] = Type;
		  ["Element"] = Element;
		  ["Desc"] = Desc;
		  ["Mana"] = Mana,
		  ["Stamina"] = Stamina;
		  ["Level"] = Level;
		  ["MaxLevel"] = MaxLevel;
		  ["Upcost"] = Upcost
	    }
	}
	
	table.insert(ServerData[p.UserId]["Skills"],#ServerData[p.UserId]["Skills"] + 1,TableD)
	print(unpack(ServerData[p.UserId]["Skills"][1]))
 end

 function module.RemoveD(p)
	ServerData[p.UserId] = nil 
 end

return module

The method is “module.AddSkill”, but for any reason dont work, the unpack dont print nothing

I already printed the values and they arent nill "print(Name…Type…Element…Desc…Mana…Stamina…Level…MaxLevel…Upcost)"

I dont know what iam doing wrong, please helpme me

2 Likes

Try replacing:

table.insert(ServerData[p.UserId]["Skills"],#ServerData[p.UserId]["Skills"] + 1,TableD)

with:

table.insert(ServerData[p.UserId]["Skills"],TableD)

because it automatically places it at the end for you.


The reason it prints nothing is because there is nothing in the skills table

["Skills"] = { 

};

Edit: oops, I read wrong, sorry

nothing, but after i pute on table.insert is supposed to have

still dont print nothing

I think the reason is that in TableD, there is another table inside it, so I would recommend printing this line

print(unpack(ServerData[p.UserId]["Skills"][1][1]))

I don’t have studio with me right now so I’m not really sure if it’s right or not.

Still dont work

Can you loop through the table and see if there is anything printed?

for i, v in pairs(ServerData) do
    print(v)
    for i, w in pairs(v) do
        print(w)
        for i, x in pairs(w) do
            print(x)
        end
    end
end

lol worked, but for any reason dont work on unpack and if i make that

1 Like

As supposed to be [“PureBlaster”] the name

To help you, here’s a documentation about tables on the Developer Hub. And you can probably figure out why table.unpack wasn’t working. Surprisingly, I learned something about unpack today.

But print isnt working too that make no sense

The reason it won’t print is because either there was an error, or there was an yield in the script that prevented it from printing. For example, wait() is a yield.

If that is true, then shouldn’t there be a warning in the output tab saying an infinite yield is active? I see no warning in the photos @TenerPVPs has sent

No, there isn’t. That’s only for yields that are infinite, but I meant temporary yields, hence the example of wait().

I use DataStore2… it’s been a godsend and allows you to easily save large amounts of data effortlessly. I just start with a basic module to set up my dozens of values, and sub values, and then through the rest of my systems, I can just call the datastore, and save whatever i need to in a few lines.

1 Like

the problem here is insert table into table, isnt the datastore

need bee [1]["PureBlaster’’], have any way to put PureBlaster as index instead the number 1?

I don’t know what you mean, can you explain a bit?

he means he wants a table to be inside another table