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

Data store confusion?

Asked by 9 years ago
After I've been learning the DataStore recently, the block of code represented an example in the wiki, it said
local DataStore = game:GetService("DataStoreService"):GetDataStore("Points")

game.Players.PlayerAdded:connect(function(player)
    local key = "user_" .. player.userId
    --we want to give 50 points to users each time they visit
    DataStore:UpdateAsync(key, function(oldValue)
        local newValue = oldValue or 0 --oldValue might be nil
        newValue = newValue + 50
        return newValue
    end)
end)

Above shows a DataStore I'm still not sure "How" to use it or where to place it, I'm sure you can use DataStore in a KO and WO in a leaderboard, but I don't know how to place it to store your "WO" and "KO" data. So please help me. Your answer requires a specific answer because I'm not an advanced scripter and I'm beginning to learn this lesson.

0
This is a good guide: http://wiki.roblox.com/index.php?title=Data_store  If you want to store more than one value then you have to use Tables. SirNoobly 165 — 9y

1 answer

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

Data Store is a Roblox Service that allows game developers to save data to the cloud. You can easily send or receive this Data using SetAsync() or GetAsync(). The advantage to using Data Store than Data Persistence is that you can load information from any server without the player needing to be there.

Data Store works as a key and value. Notice below how each key has a value. A better image of how its saved is here.

"Hello"  | 9999
"Bye"    | 50
"Stay"   | 100

In order to use this service, you would want to call for it;

DataStore = game:GetService("DataStoreService"):GetDataStore("StatsService")

You can name StatsService to anything you want, I just believe that it should possess a relevant name to what you are doing.


In your case, in order to use the script to load WipeOut and KnockOuts leaderboard you would use :GetAsync().

game.Players.PlayerAdded:connect(function(player)
    repeat wait() until player:FindFirstChild("leaderstats") --Script will wait until leaderstats exists.
    repeat wait() until player.leaderstats:FindFirstChild("WipeOuts") --Script will wait until WipeOuts exist.
    repeat wait() until player.leaderstats:FindFirstChild("KnockOuts") --Script will wait until KnockOuts exist.

    local PlrsWipeOuts = DataStore:GetAsync(player.Name .. "_WOs") --Script will look for a key or identifier named "Player1_WOs" or what player joined the game.
    local PlrsKnockOuts = DataStore:GetAsync(player.Name .. "_KOs") --Script will now look for "Player1_KOs"

    --Now that we have the information we can set the values to the leaderstats.
    if PlrsWipeOuts ~= nil then --If it is nil, the player has never been to your game.
        player.leaderstats.WipeOuts.Value = PlrsWipeOuts
        player.leaderstats.KnockOuts.Value = PlrsKnockOuts --If the DataStore got a number for WOs then it should get a number for KOs.
    end
end)

You can save data by using the .PlayerRemoving Event and the SetAsync() function.

Warning, now PlayerRemoving has been known to be buggy when say, the server lags out. So if your game does not require players getting killed easily you can use a .Changed Event.

game.Players.PlayerRemoving:connect(player) --Script detected a player is leaving.
    if player:FindFirstChild("leaderstats") then --If leaderstats still exists, continue...
        if player.leaderstats:FindFirstChild("WipeOuts") then --If WipeOuts still exist then continue...
            DataStore:SetAsync(player.Name .. "_WOs", player.leaderstats.WipeOuts.Value) --Save the value for WipeOuts under the key "Player1_WOs"
        end
        if player.leaderstats:FindFirstChild("KnockOuts") then --Same thing for Knockouts.
            DataStore:SetAsync(player.Name .. "_KOs", player.leaderstats.KnockOuts.Value)
        end
    end
end)

Now you have saved the data.


For your script, I am not too familiar with UpdateAsync but I do believe anyone can get by on just using Get/SetAsync()


I hope this helped, if you have any questions feel free to ask on this site. I will ignore scripting questions to me on the ROBLOX site.

Ad

Answer this question