I need some examples on how the codes are written to insert a table, remove a table, and add a value to the table. The wiki is great and all but words don't really explain it that well to me.
How does Datastore and tables work exactly?
1 answer
Firstly, you need to think of a table like a list, with the "index" and the "value". The index is the position in the table, so if you picture it like the following:
local NameOfTable = {
1 = Value1,
2 = Value2,
3 = Value4,
4 = Value5
}
The index, starting with 1 onwards, shows the position, and the Value is what you put in the table.
The values can be anything: numbers, strings, vector3s (etc.) and even other tables!
Now, onto indexing...
Lets use the following table as an example:
local Table1 = {
"lol",
"this",
"is",
"really",
"fun"
}
At the moment, I only have strings in my table (to hopefully make it easier to understand).
To grab something out of the table, I can use the index:
print(Table1[2]) --This will print the second value in the table (i.e. "this")
I can also change values using the index:
Table1[1] = "I know"
This will change the first value (i.e. "lol") to "I know"
Another way to get the index (if you don't know) is to use a loop:
for index,Value in pairs(Table1) do
print(index, "=", Value)
end
This will print the following into the output:
1 = I know 2 = this 3 = is 4 = really 5 = fun
To insert a value into the table at the end, you can use:
table.insert(Table1, "lol") --This will add a new value at the end of Table1
OR
Table1[#Table1+1] = "lol" --Essentially by using an index at the end of the table
To remove the value:
table.remove(Table1, 6) --6 being the index of what you want removed
OR
Table1[6] = nil --nil being the default for an empty value
String indexes:
Table1["Hi"] = "Troll"
print(Table1["Hi"]) --Prints "Troll"
"How do datastores work with tables?"
I think I know what you mean by this, so the first thing you need to know is the datastore functions.
You will need to grab your datastore first by using the GetDataStore method on the DataStoreService:
local Datastore = game:GetService("DataStoreService"):GetDataStore("SomeRandomName")
"SomeRandomName" is the name of the datastore which you want to put your data into.
Next, the actual get/set/update methods:
SetAsync - The method of "saving" data to the store
local Value = 20 --What I want to save to my datastore
local Key = "SomeOtherRandomThing" --Up to 50 characters
Datastore:SetAsync(Key, Value) --This will save the value into the datastore under the name of "SomeOtherRandomThing"
GetAsync - The method of "loading" data from the store
local Key = "SomeOtherRandomThing" --Same as previous
local Value = Datastore:GetAsync(Key)
print(Value) --prints 20
UpdateAsync - The method of getting the current data and replacing it
local Key = "SomeOtherRandomThing" --Same as previous... again
Datastore:UpdateAsync(Key, function(OldValue)
print(OldValue) --prints 20
return OldValue +20
end)
...And if I was to use GetAsync again and print the value, it would print 40.
Now to answer your question
You wanted to manage tables in your datastore? Well, that's not much different:
I'd firstly recommend you use UpdateAsync if you are simply adding/removing entries.
local Datastore = game:GetService("DataStoreService"):GetDataStore("SomeRandomName")
local Key = "ExampleKey"
Datastore:SetAsync(Key, {"Hi", "lol"})
wait(5)
local NewEntry = "Hi there"
Datastore:UpdateAsync(Key, function(CurrentTable)
table.insert(CurrentTable, NewEntry) --Adds a new value to the table
return CurrentTable --Updates it
end)
And you can use a similar code to remove the values from the table (using table.remove)
To completely clear the table, just do:
Datastore:SetAsync(Key, nil) --To make it empty
OR
Datastore:SetAsync(Key, {}) --To make it an empty table
I hope this answered your question, and as I've said before, PM me if you have any questions...
2 comments
Yes this is for a table, but i mean how to set a table in datastore, because it isnt flat out a = v, it's a:SetAsync(k,v). I want to be able to clear the entire table inside a datastore.
Edited it
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
The wiki is great and all but if you don't understand how to write it then you're simply not learning.
I actually learn alot by trial and error and by using, but "tableName[key] = value"doesn't make any sense to me because when dealing with datastore you arent using the standard a = v but a:SetAsync(k,v)
How do* DataStores* and tables work together*?