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

How does GlobalDataStores, GetAsync work?

Asked by
woodengop 1134 Moderation Voter
9 years ago

I did look it up on Wiki but it didn't help at all, Could any of you please explain why and how this works?

1 answer

Log in to vote
4
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

GlobalDataStores are essentially used for getting OrderedDataStores. OrderedDataStores are similiar to DataStores, but when using the UpdateAsync / SetAsync / IncrementAsync functions the value for the set key must be an integer. And they can be used to return a sorted DataStore - e.g. highest values first, lowest last. Ideas for using this are global leaderboards for your game. Like top ten players or something like that(:


To make a GlobalDataStore, you have to use the GetOrderedDataStorefunction of DataStoreService, with an argument of either a custom name, a scope, or 'global'

local ds = game:GetService('DataStoreService'):GetOrderedDataStore('global')

You have now created a global datastore! Now to get results like you see in other games(with lists of players and their values) you need to use the GetSortedAsync method. This should have arguments of the PageSize(how many results per page) - The mazimum page size is 100, the minimum value, and the mazimum value.


You can Set / Update / Increment keys just as you would normal datastores


You can go to the next page of the results by using the AdvanceToNextPageAsync method.


You can get the current page you're on by using the GetCurrentPage method.


And lastly, you can check if it's the last page by using the IsFinished property. This property is read-only.


Example;

--DataStoreService
local dss = game:GetService("DataStoreService")
--OrderedDataStore
local ods = dss:GetOrderedDataStore("HighScores")

--Set Some values
ods:SetAsync('Goulstem',100)
ods:SetAsync('1waffle1',50)
ods:SetAsync('Noob',0)

--Get the pages of the datastore, with a limit of results as 1
local pages = ods:GetSortedAsync(false, 1) 
--Set a currentPage variable, to determine when it's finished
local currentPage = 0

while wait() do
    --Get the current page's data
    local data = pages:GetCurrentPage()
    --Iterate through the data, print it out
    for i,v in pairs(data) do
        print(k .. " " .. v.key .. ": " .. v.value .. " points")    
    end
    --Check if all the pages have been gone through
    if pages.IsFinished then
        --If they have then break the loop
        break
    end
    --Otherwise, advance to the next page, and increment the variable
    pages:AdvanceToNextPageAsync()  
    currentPageNumber = currentPageNumber + 1
end

You can read an overview on this subject here


--EDIT--

REGULAR DATASTORE SAVING

This is a widely used aespect to the Lua 5.1 API. It's used for saving anything over servers, mainly player stats. You setup a DataStore by using DataStoreService, with the GetDataStore function. The arguments for this function should be the name of your DataStore. If you were to change this name then the DataStore would reset.

You use the GetAsync function to obtain previously saved data from a specified key. If no data was saved into that key it will return nil.

You use the SetAsync function to write, or save, data to DataStores in a specific key, so that it can be obtained later on.

While setting the key for Data, you're most likely going to be wanting to save data for the player - so you'll want to use something involving the player you're saving the data for inside the key. You can use their name but I highly recommend not doing this due to the fact that robloxian usernames are dynamic. So I suggest using their userId. You can get this from a property in their player instance called 'userId'.

So when a player joins, we want to check if they have any data saved.. if they do then set it to some leaderstats. If they don't then just don't do anything(:

The saving comes in when a player leaves the game. When a player is leaving the game(detected by the PlayerRemoving event) then you use the UpdateAsync function to save their data.

Example;

--Define the datastore
local ds = game:GetService('DataStoreService'):GetDataStore('Points')

--When a player joins
game.Players.PlayerAdded:connect(function(plr)
    --define the key to be used
    local key = plr.userId..'_data'
    --get potential data
    local data = ds:GetAsync(key)
    --see if data exists
    if data then
        --if it does then set it to a leaderstat
    end
end)

--when a player leaves
game.Players.PlayerRemoving:connect(function(plr)
    --get the key to be used
    local key = plr.userId..'_data'
    --save data to the key, with the value as a leaderstat.
    ds:SetAsync(key, plr.Points.Value)
end)
0
can this also be used to create a "save" and load"? woodengop 1134 — 9y
1
It can! But that's no different from regular DataStores so I didn't think I needed to point that out(: Goulstem 8144 — 9y
0
I don't mean to waist your time, but how would you do that? Maybe an Example script. woodengop 1134 — 9y
1
I'll edit my answer. Goulstem 8144 — 9y
View all comments (2 more)
1
There, I added a stat saving guide to the bottom of my answer(: Goulstem 8144 — 9y
0
Thank you! woodengop 1134 — 9y
Ad

Answer this question