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
10 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
10 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

1local DataStoreService = game:GetService('DataStoreService')
2local SavedData = DataStoreService:GetDataStore('Kills')
3local 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.

01local key = player.userId
02 
03--IncrementAsync
04 
05SavedData:IncrementAsync(key, 1)
06--Adds onto already existent value
07 
08--UpdateAsync
09SavedData:UpdateAsync(key, function(oldValue)
10    newValue = oldValue or 0 -- if no previously saved data, set new to 0
11 
12    return newValue -- associated value "newValue" to key
13end)
14 
15--SetAsync
16SavedData:SetAsync(key, 0)
17 -- overrides all previously saved values
18-- 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 — 10y
Ad
Log in to vote
3
Answered by 10 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)

1local 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

01local data = game:GetService("DataStoreService"):GetDataStore("Test")
02print("The Brick's color is: "..data:GetAsync("Color"))
03--[[How to use it:
04data is the DataStores that you got, data is the datastore "Test".
05 
06:GetAsync("Key")
07Is a function. Must be used on the appropriate datastore.
08The key is like a Model or a Folder. It's like doing :FindFirstChild() in the Model or Folder
09It returns the value. Except there is only one value.
10 
11This is what it would look like if it was a model:
12 
13local data = workspace.Model
14 
15data:FindFirstChild("Part")
View all 21 lines...

SetAsync

It gives or changed the key's value.

1local data = game:GetService("DataStoreService"):GetDataStore("Test")
2data:SetAsync("Color", "Red")
3print("The Brick's color is: "..data:GetAsync("Color")) --Prints "The Brick's color is: Red"
4data:SetAsync("Color", "Blue")
5print("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.

01local data = game:GetService("DataStoreService"):GetDataStore("Test")
02 
03data:UpgradeAsync("Points", function(old)
04    local new = old or 0 --In Case "old" is nil.
05    new = new+10
06    return new --Must have a return.
07end)
08 
09--Another Example:
10 
11data:UpgradeAsync("Points", function(old)
12    return 50 --Immediately changes points to 50.
13end)

IncrementAsync

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

--Wiki

1local data = game:GetService("DataStoreService"):GetDataStore("Test")
2 
3data: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.

01local player = game:GetService("Players").LocalPlayer
02 
03print(player.Name.." had this many points: "..player:LoadNumber("Points"))
04player:SaveNumber("Points", 50)
05print("Now the player has "..player:LoadNumber("Points").." points!") --Which is 50
06--[[Save/Load works similar to SetAsync and GetAsync.
07BTW:
08SaveNumber ONLY saves numbers
09SaveString ONLY saves strings
10SaveBoolean ONLY saves Bools
11SaveInstance ONLY saves RBLX objects
12]]

Tables, Arrays, Pairs

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

1Table = {} --You can add stuff like this:
2Table2 = {"Hai", "Hello", "Wassup", 21, true, false, 54, workspace.Part:clone(), ":3"}
3--You can add using table.insert()
4table.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:

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

Pairs works by...

1--returning everything in a table or array!
2for i,v in pairs(table) do
3end
4--"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
10 years ago

CodeTheorem made a guide to it here.

One does not simply explain DataStores briefly.

Answer this question