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

Simple Datastore introduction?

Asked by 8 years ago

How do Datastores work? Like if I want to have a gui that people can suggest something I can receive the data later.

0
Roblox has a wiki on DataStore and DataPersistance NinjoOnline 1146 — 8y

1 answer

Log in to vote
5
Answered by 8 years ago

Why use data store?

The "Datastore" service was devised by ROBLOX with intent to replace the old method of storing data, which was known as data persistence. This new service for storing data (data store), is not only incredibly well organized, but can also store data on a scale much larger than data persistence.

What is data store?

As the name implies (obviously), this service is used to store data outside of ROBLOX servers to recollect whenever needed. However, there are limitations to what can and can't be stored using this service. I'll go over those later in this explanation.

How does data store work?

It's very important to know how this services stores data the way it does. This will help you debug a lot of your code, or even avoid bugs in the first place.

Data store holds data in a format known as a dictionary. A dictionary is a basic term in data structures which implies you're storing something with both a defined key, and corresponding value. We can create a visual example of this using a simple table:

local Dictionary = {
    ["Key"] = "Value"
}

In which our table is written in dictionary format because we have an assigned key ("Key"), and a value ("Value").

You can basically think of data store as putting files filled with paper in your drawer. Where the files represent the dictionaries, your papers *represent the *keys, and the content on those papers represent the values. This is exactly how data store holds data.

Here's a visual demonstration with my beautiful art work: http://prntscr.com/9k4hrl

CREATING a data storage

Now to put all of this information into use. As the analogy above implies, we're going to need a "file" to start out with to store our keys and values. We can create these "files" using a method on the DataStoreService called GetDataStore. Here's an example:

-- Get the data store service
local DataStoreService = game:GetService("DataStoreService")

-- Create a new "file" in which we can store information in.
local GameData = DataStoreService:GetDataStore("ServerInfo")

"ServerInfo", the first and only argument in our GetDataStore function, is the name of our newly created file. Note that this will NOT create a new data store file if the name you give it is pre-existing. Making this a very easy-to-use way of creating new data storages.

SAVING to a data storage

Now that we have a data storage, we can start saving some information to it. To do this, we can use the SetAsync method on our chosen data store file, and save our dictionary-styled column of data to it. SetAsync works like this:

-- Get the data store service
local DataStoreService = game:GetService("DataStoreService")

-- Create a new "file" in which we can store information in.
local GameData = DataStoreService:GetDataStore("ServerInfo")

-- Save some data to it
-- Notice this method is being used on "GameData", our newly created data file.
GameData:SetAsync("DataKey", 100)
-- We've just saved a value of "100", under the data key: "DataKey", inside the data file of: "ServerInfo".

UPDATING data to a storage

Now, while we could use SetAsync over and over to update data that we already have stored, it's a very poor way of doing so. To ensure best results of updating existing data to a new value, is to use the UpdateAsync method (again, on the data file we created). However, the UpdateAsync function works a bit differently. Here's an example:

-- Get the data store service
local DataStoreService = game:GetService("DataStoreService")

-- Create a new "file" in which we can store information in.
local GameData = DataStoreService:GetDataStore("ServerInfo")

-- This is assuming we already saved "DataKey" to this file from the above example:
GameData:UpdateAsync("DataKey",function(OldValue)
    -- OldValue should be 100 (from above example)
    if type(OldValue) == "number" then

        -- Now we increase it by 1 (now 101)
        OldValue = OldValue + 1
    end

    -- And now we return the changed "OldValue" to the data store.
    -- Our new stored data value has just updated to 101.
    return OldValue
end)

RETRIEVING data from a storage

Lastly, retrieving data from a file. For this, we can use the GetAsync method (yes, on the data file). This function only requires one argument, which is the data key. Here's how it works:

-- Get the data store service
local DataStoreService = game:GetService("DataStoreService")

-- Create a new "file" in which we can store information in.
local GameData = DataStoreService:GetDataStore("ServerInfo")

-- Save some data to it
GameData:SetAsync("DataKey", 100)

-- Returns the value saved to the key of the file.
print(GameData:GetAsync("DataKey")) -- Should print 100.

LIMITATIONS on data storage

Now as I've said at the beginning, there are certain data types that can't be stored using this service. However, it shouldn't cause many problems. One thing we need to note here, is that since the information we're storing is held out side of ROBLOX servers, we cannot use ROBLOX-specific or ROBLOX Lua specific data (userdata and functions values will not save). Here's a list of data types that can be stored using data store:

- Tables (of a single-style type format, i.e, either an array or dictionary)

- Numbers

- Strings

- Booleans

- nil

If you want to save something that cannot be stored, you should make a data parser. Something that will take information from saved data, read it as source code, and have a function compile information given the source code conditions.

Sources In case that wasn't enough, there's an entire tutorial you can find on the ROBLOX wiki right here: http://wiki.roblox.com/index.php?title=Data_store

I also suggest learning a bit more about dictionary and array styled tables, as that will give you a better understanding on how to store data properly. Hope this helped, let me know if you have any questions.

0
This is a great guide about DataStores! AlphaGamer150 101 — 7y
Ad

Answer this question