Hi there! I'm trying to mess around with different ways for storing data via DataStore without saving each value individually. Someone I know had suggested trying to save it all with a table, so I'm trying to do that. However, I'm a bit confused as to how I would use table.insert(), or anything else to add the name of the value, and the value itself to the table? Any help is greatly appreciated. :)
What I want:
1 | local Table = { |
2 | [ "Cash" ] = 0 |
3 | } |
Script I've tried using:
1 | local Table = { } |
2 | for _,v in pairs (Player [ "leaderstats" ] :GetChildren()) do |
3 | table.insert(Table, { [ v.Name ] = v.Value } ) |
4 | end |
Its simple, dont need to use table.insert
you can only use Table[ITEM] = VALUE
This basically creates a new value in a table. because you're basically setting this for the table.
Here is a example:
1 | local Table = { } |
2 |
3 | Table [ "Coin" ] = 120 -- Add the value to table. |
4 | wait() |
5 | print (Table [ "Coin" ] ) -- Print the value. |
Here is your fixed script:
1 | local Table = { } -- Items table |
2 |
3 | for _,v in pairs (Player [ "leaderstats" ] :GetChildren()) do -- Get all items in leaderstats in player |
4 | Table [ v.Name ] = v.Value -- Add the values to table. |
5 | end |
Hope it helped :)