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

Better explanation on DataStore//Persistence?

Asked by
unmiss 337 Moderation Voter
9 years ago

I don't really understand HOW to use DataStores as said on the wiki page: http://wiki.roblox.com/index.php?title=Data_store

Could someone explain how to use it correctly? If possible, provide some sort of brief example (please don't downvote :c)? I must use it for something but honestly just don't get it.

Nor do I understand how to use the tables/pairs with it.

3 answers

Log in to vote
3
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

Methods

  • IncrementAsync
  • UpdateAsync
  • GetAsync
  • SetAsync

IncrementAsync increments the value by a specified amount.

SetAsyncis pretty much similar to UpdateAsync and IncrementAsync in the sense that they both change the DataStore.

However, if you're interested in getting previously saved values in your querry, you would want to useUpdateAsync because it will get the already existing value of the key, run the transformFunction (aka second argument), and then updating the key. SetAsync, is mostly used for constant values, and overrides the value of the key with the newly set value.

GetAsync just simply means fetching the value associated with the provided key


Accessing the DataStoreService

local DataStoreService = game:GetService('DataStoreService')
local SavedData = DataStoreService:GetDataStore('Kills')
local Players = game:GetService('Players')

There's an additional optional parameter, "scope", to the GetDataStore method, that simply assigns the data to the specified scope. It is set to "global" by default.

Saving to the DataStoreService

Since SavedData is the reference to the data store, it would make sense to use it when saving or getting values. You would need a unique identifier to save values specific to players. It is recommended, and much easier to use their userId property when doing so.

local key = player.userId

--IncrementAsync

SavedData:IncrementAsync(key, 1)
--Adds onto already existent value

--UpdateAsync
SavedData:UpdateAsync(key, function(oldValue)
    newValue = oldValue or 0 -- if no previously saved data, set new to 0

    return newValue -- associated value "newValue" to key
end)

--SetAsync
SavedData:SetAsync(key, 0)
 -- overrides all previously saved values
-- doesn't check for previous values

The second argument of the UpdateAsync method, is the "transformFunction", as defined above.

0
I hate it when people ninja me! I will keep mine up still though, since he asked help for DataStores, Pairs, and also maybe some data persistance. EzraNehemiah_TF2 3552 — 9y
Ad
Log in to vote
3
Answered by 9 years ago

DataStores, It's for saving stuff, you already know. But I'll show you how to use it!


GetDataStore

This is the most important thing when using DataStores(Other than the DataStoreService)

local data = game:GetService("DataStoreService"):GetDataStore("Test") --Name of the datastores is "Test". This is where you start when you're using DataStores.

One DataStore can have multiple keys.


GetAsync

This get's the value for the key

local data = game:GetService("DataStoreService"):GetDataStore("Test")
print("The Brick's color is: "..data:GetAsync("Color"))
--[[How to use it:
data is the DataStores that you got, data is the datastore "Test".

:GetAsync("Key")
Is a function. Must be used on the appropriate datastore.
The key is like a Model or a Folder. It's like doing :FindFirstChild() in the Model or Folder
It returns the value. Except there is only one value.

This is what it would look like if it was a model:

local data = workspace.Model

data:FindFirstChild("Part")

The DataStores is like a model, GetAsync is like FindFirstChild and the key is like the name of the object you're trying to find.

Something else to remember:
Just like FindFirstChild, if there is no data inside the key(or if there is no "part"[If you're using the FindFirstChild example]) it will return, nil.
]]

SetAsync

It gives or changed the key's value.

local data = game:GetService("DataStoreService"):GetDataStore("Test")
data:SetAsync("Color", "Red")
print("The Brick's color is: "..data:GetAsync("Color")) --Prints "The Brick's color is: Red"
data:SetAsync("Color", "Blue")
print("The Brick's color is: "..data:GetAsync("Color")) --Prints "The Brick's color is: Blue"

UpgradeAsync

Some people say this should be used more than SetAsync. It does almost the same thing as SetAsync but it is in a form of a function.

local data = game:GetService("DataStoreService"):GetDataStore("Test")

data:UpgradeAsync("Points", function(old)
    local new = old or 0 --In Case "old" is nil.
    new = new+10
    return new --Must have a return.
end)

--Another Example:

data:UpgradeAsync("Points", function(old)
    return 50 --Immediately changes points to 50.
end)

IncrementAsync

Increments the value for a particular key and returns the incremented value. Only works on values that are integers.

--Wiki

local data = game:GetService("DataStoreService"):GetDataStore("Test")

data:IncrementAsync("Number", 1)

I've never used IncrementAsync so ask someone like BlueTaslem, Goulstem, Redbullusa, adark, etc.


DataPersistance

Should not be used because DataStores is better. DataStores can save within universes. DataPersistance does not.


Save/Load Numbers, Strings, Booleans, Instance

Can only be saved and loaded from a player.

local player = game:GetService("Players").LocalPlayer

print(player.Name.." had this many points: "..player:LoadNumber("Points"))
player:SaveNumber("Points", 50)
print("Now the player has "..player:LoadNumber("Points").." points!") --Which is 50
--[[Save/Load works similar to SetAsync and GetAsync.
BTW:
SaveNumber ONLY saves numbers
SaveString ONLY saves strings
SaveBoolean ONLY saves Bools
SaveInstance ONLY saves RBLX objects
]]

Tables, Arrays, Pairs

Tables is a list of strings, numbers, bools, and objects:

Table = {} --You can add stuff like this:
Table2 = {"Hai", "Hello", "Wassup", 21, true, false, 54, workspace.Part:clone(), ":3"}
--You can add using table.insert()
table.insert(Table, "Hello", 1) --The 1 is optional, it says what slot it should go in. You can look up other things you can do with tables on the wiki!

Arrays are very similar:

Array = {"Hello", 23}
Array2 = {
{thing = "Hello"}
{no = "false"}
}
Array3 = {Hi = 2, No = false, Error = "5/0"}

Pairs works by...

--returning everything in a table or array!
for i,v in pairs(table) do
end
--"i" returns what place it is in the table and "v" is the value, like "Hello" or 21 and etc.


Hope this helps!

Log in to vote
1
Answered by
drew1017 330 Moderation Voter
9 years ago

CodeTheorem made a guide to it here.

One does not simply explain DataStores briefly.

Answer this question