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

Can someone make a simplified data store guide?

Asked by
Kasumo 2
7 years ago

I have tried to watch videos and have read the wiki, but I have yet to learn datastore. Could someone make me a very simplified guide/tutorial on how to use it? (Please no fancy scripting terms, I would really like it dumbed down)

So far, I know SetAsync() is the saving, and GetAsync() is the loading

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Well, let me explain you how it works!

You will start with making a bool to the data storage. Easy...


DataStore = game:GetService("DataStoreService")

Once you have connect the DataStorageService to the bool. You have been granted to functions of the bool. These functions are...

:GetAsync :SetAsync ect.

If you really want to know the basics of data storage? Then learn these functions and their meanings.

Datastorage functions. +Meanings

^ Read them well. And then start scripting with them. Just test it on saving one simple value. If you want more help the link here under might help you.

Data Storage, examples and tutorials.

If you really don't understand it, then go to a scripter and ask of they could do it for you.

Just some tips, I hope I have helped you today! Good luck.

0
What? A boolean is not what you're referring to it as, a boolean is a binary state with a true or false value. ScriptGuider 5640 — 7y
0
My bad. It should be just a "Value" Difined_Person 61 — 7y
Ad
Log in to vote
1
Answered by 7 years ago

SetAsync() DIRECTLY sets the value and the old value will be lost. However, there is a function called UpdateAsync(). This function "transforms" the old value into a new one. Here is an example:

This script adds 10 coins to the player's data upon leaving.

local dss = game:GetService("DataStoreService"):GetDataStore("Coins")

---This focuses on the player leaving...

game.Players.PlayerRemoving:Connect(function(plr)
    local key = "Plr-"..plr.UseId -- Required when saving.

    dss:UpdateAsync(key, function(oldValue) -- When using UpdateAsync, the first parameter is the KEY, the second parameter is the function that updates the value for the key. The function in the second parameter will have a returned value that is OldValue (the old data).
        local newValue = oldValue or 0 -- oldValue might be nil.
        return newValue + 10 -- It MUST return something!!!!!
    end)
end

Using UpdateAsync() is okay when you are going to use the OldValue for updating or doing anything upon updating. However, there is a shorter way ONLY when the data value is an integer.

And the way is to use IncrementAsync()!

Using the code from above, let's do this the easiest way!

local dss = game:GetService("DataStoreService"):GetDataStore("Coins")

---This focuses on the player leaving...

game.Players.PlayerRemoving:Connect(function(plr)
    local key = "Plr-"..plr.UseId -- Required when saving.

    dss:IncrementAsync(key, 10) -- Adds 10 to the value of the key's entry. The first parameter is obviously the key, and the second parameter is how much you want the value to add.
end

These two methods are very good ways to update your data! Also, If you wanted to organize you data storage, there is a second parameter named scope which has a default value of global. It is like an organizer that belongs to the same category. For example, local dss = game:GetService("DataStoreService"):GetDataStore("Coins") IS NOT THE SAME AS local dss = game:GetService("DataStoreService"):GetDataStore("Coins", "Silver") They are different because their SCOPES are different! That's how you can store different types of data in a datastore. Make sure to reference the Scope in the dss variable (script above) so you can also GetAsync from it.

Don't forget that there are limits applied to saving data, so make sure that the game does not try to update data too often, or else you will be flooded with request rejected errors!

That's all I can tell you about Data Stores. If you have any questions, please comment below. Thanks and have a nice day!

Answer this question