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

Data Store Help?

Asked by 9 years ago

I haven't attempted to use Data Store yet, and I'd like to get familiar with it. I've looked at wikis and other questions and don't really understand it. Here's my script to attempt using data stores:

local DataStore = game:GetService("DataStoreService")

game.Players.PlayerAdded:connect(function(player)
    local key = "user_" .. player.userId
    DataStore:UpdateAsync(key, function(oldKills)
        local Kills = oldKills or 0
        Kills = Kills + 50
        return Kills
    end)
end)

game.Players.PlayerRemoving:connect(function(player)
    local key = "user_".. player.userId
    local KOs = (tostring(player.leaderstats.Kills))
    DataStore:SetAsync(key,KOs)
end)

As I said I don't at all understand this, and don't know how I got to this point. What are the arguments with the UpdateAsync, SetAsync, and GetAsync functions?

1 answer

Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

Your code is pretty close to being accurate except the fact that you might want to save the value when the player leaves rather than joins the server, using UpdateAsync. Also, SetAsyncis pretty much similar to UpdateAsync 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 OnClose ensures that ROBLOX's server cannot shutdown until the provided Callback(argument) has finished. As of now, the Callback can only yield for a maximum of 30 seconds. I would recommend using 5 seconds in this context.

In other words, UpdateAsync would be best in this context.


  • Get new data store
local DataStore = game:GetService("DataStoreService")
local SavedData = DataStore:GetDataStore("PlayerKills")
local Players = game:GetService("Players")
Players.PlayerAdded:connect(function (player)
    local key = ('user_'..player.userId)
    local saved = SavedData:GetAsync(key) or 0

    local leaderstats = Instance.new('Folder')
    leaderstats.Parent = player
    leaderstats.Name = 'leaderstats'

    local kills = Instance.new('NumberValue')
    kills.Name = 'Kills'
    kills.Parent = leaderstats
        kills.Value = saved
end)
  • Take care of leaving players, using the PlayerRemoving event (RBXScriptSignal)
Players.PlayerRemoving:connect(function (player)
    local key = ('user_'..player.userId)
    local stats = player:WaitForChild('leaderstats')
    local kills = stats:WaitForChild('Kills')

    SavedData:UpdateAsync(key, function(oldKills)
        local newKills = oldKills or kills.Value
        print('new',newKills)
        return newKills
    end)
end)
  • Onclose
game.OnClose = function() wait(5) end

Altogether..


local DataStore = game:GetService('DataStoreService')
local SavedData = DataStore:GetDataStore('PlayerKills')
local Players = game:GetService('Players')

Players.PlayerAdded:connect(function (player)
    local key = ('user_'..player.userId)
    local saved = SavedData:GetAsync(key) or 0
    print('old',saved)
    local leaderstats = Instance.new('Folder')
    leaderstats.Parent = player
    leaderstats.Name = 'leaderstats'

    local kills = Instance.new('NumberValue')
    kills.Name = 'Kills'
    kills.Parent = leaderstats
    kills.Value = saved
end)

Players.PlayerRemoving:connect(function (player)
    local key = ('user_'..player.userId)
    print(key)
    local stats = player:WaitForChild('leaderstats')
    local kills = stats:WaitForChild('Kills')

    SavedData:UpdateAsync(key, function(oldKills)
        local newKills = oldKills or kills.Value
        print('new',newKills)
        return newKills
    end)
end)

game.OnClose = function() wait(5) end

more on data stores here

0
returns GetAsync is not a member of DataStoreService BSIncorporated 640 — 9y
0
Oh, I forgot to actually create or reference a data store using `GetDataStore` ImageLabel 1541 — 9y
0
I'm still having a lot of issues, no output errors BSIncorporated 640 — 9y
0
what kinds of issues? ImageLabel 1541 — 9y
0
I had a few issues including the fact that both events were `PlayerAdded` instead of one being `PlayerRemoving`. Also, I tried to save an instance instead of it's value in the UpdateAsync function. Fixed. ImageLabel 1541 — 9y
Ad

Answer this question