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

How to use the data saved in a datastore?

Asked by 5 years ago

Hello everyone, I found a datastore script:

local DataStoreService = game:GetService("DataStoreService")
local statStore = DataStoreService:GetDataStore("StatStore")

game.Players.PlayerAdded:Connect(function(Player)
        local leaderstats = Instance.new("Folder", Player)
        leaderstats.Name = "leaderstats"

        local cashs = Instance.new("IntValue", leaderstats)
        cashs.Name = "Money"
        cashs.Value = 2000

        local death = Instance.new("IntValue", leaderstats)
        death.Name = "Death"
        death.Value = 5

        local recieved, NotRecieved = pcall(function()
            stat = statStore:GetAsync("UserId-"..Player.UserId)
        end)

        if recieved then
            cashs.Value = stat[1]
            death.Value = stat[2]
            print("Stat was recieved")
        else
            print("Stat wasn't recieved")
        end

        while true do wait(30)
            statStore:SetAsync("UserId"..Player.UserId, {cashs.Value, death.Value})
        end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local leaderstats = Player.leaderstats
    local money = leaderstats.Money
    local death = leaderstats.Death

    local saved, notSaved = pcall(function()
        statStore:SetAsync("UserId-"..Player.UserId, {money.Value, death.Value})
    end)

    if saved then
        print("Stat was saved")
    else
        print("Stat wasn't saved")
    end
end)

With this datastore script, how can I use the data saved in other scripts, for example, I want to display the value of the money and death saved in a text label. How to do this? PS: I'm a beginner

1 answer

Log in to vote
0
Answered by
starmaq 1290 Moderation Voter
5 years ago
Edited 5 years ago

:SetAsync() is the method responsible for saving the data, and :GetAsync() is the one responsible for loading or retrieving the data back. Let's say you saved a value using :SetAsync(). Remember that this method has 2 paramaters: A unique key that serves as a special name for the save value, and the value that you wanna save. And notice how I said unique key! That's why all example use the plr.UserId, because each player has his own specail ID that can never be found in another player, which makes each player have his own saved data without it being overwritten by another player. If we used the same key, the same value will be replaced whenever any player tries to save his data.

And also, you're commonly going to be saving the data (:SetAsync()) when the player leaves, meaning we will use the .PlayerRemoving event. You can also make an autosaving system, by placing the :SetAsync() inside of a while loop that loops every 5 mintues for examples. But let's just stick the simple player removing.

And also let's say this player has some leaderstats, like money, which are the ones that we wanna save.

Anyways, this is probably what you're gonna do


local DataStoreService = game:GetService("DataStoreService") local statStore = DataStoreService:GetDataStore("StatStore") --makec( a datastore game.Players.PlayerRemoving:Connect(function(plr) --this event has the player that left as a paramater statStore:SetAsync(tostring(plr.UserId), plr.leaderstats.Money) --plr.UserId is an integer, but we need it to be a string so we tostring() it end)

Now let's use the retrive data part using :GetAsync(), and what's very important is: You have to use the same key for the value that you saved!


local textbox = script.Parent --or wherever it is textbox.Text = tostring(statScore:GetAsync(tostring(plr.UserId)) --just the key, and this should return the saved value, which is the money

And also logically, you wanna be retrieving the data when the player joins, using the .PlayerAdded event. Which fires when the player joins.

Ad

Answer this question