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

ROBLOX String Value Datastore?

Asked by 9 years ago

I probably know the answer already, but why doesn't datastore save a string value?

0
It does... DataStore:SetAsync(string key, variant value) M39a9am3R 3210 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

Data store can save all values that can be interpreted by another language. This includes strings, bools, numbers, and tables. Let's try going over this step by step:

Getting data store service

local DataStoreService = game:GetService'DataStoreService'

Creating a new data store

So, data store gives us the neat option to basically create a table we can save data to. We can create such a table with using the "GetDataStore" method on the data store service, like so:

local DataStoreService = game:GetService'DataStoreService'
local MyData = DataStoreService:GetDataStore'Testing'

If "Testing" already exists, it will retrieve all data from that table. If not, it will create a new one that we can configure.

Saving data

We can save data to this newly created table, using "SetAsync" and "UpdateAsync". Let's try an example of SetAsync:

-- Data stuff
local DataStoreService = game:GetService'DataStoreService'
local MyData = DataStoreService:GetDataStore'Testing'

-- New data
MyData:SetAsync("Key1" , "Hello world")

In the example above, we set "Key1" to the value of "Hello world". In Lua understanding, we can basically say it looks like this:

local DataStoreService = {
    ['MyData'] = {
        ['Key1'] = 'Hello world'
    }
}

Now, don't take this literally, but this can be a very clear way to look at it and understand it.

Getting data

Let's test our example by printing the data we saved with "print" and "GetAsync"

-- Data stuff
local DataStoreService = game:GetService'DataStoreService'
local MyData = DataStoreService:GetDataStore'Testing'

-- New data
MyData:SetAsync("Key1" , "Hello world")

-- Print results
print(MyData:GetAsync("Key1"))  -- > "Hello world"

Let me know if you have any questions.

Ad

Answer this question