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

Transferring from Data Persistence to Data Store?

Asked by
Dummiez 360 Moderation Voter
10 years ago

Moreover on getting player leaderstats and storing them on a string on DataStore.

local DataStore = game:GetService("DataStoreService"):GetDataStore("UserStats")

game.Players.PlayerRemoving:connect(function(player)
    local key = "user_" .. player.userId
    DataStore:UpdateAsync(key, function(oldValue)
        local statData = ""
        for i, v in ipairs(player.leaderstats:GetChildren()) do
        statData = (statData.." "..v.Name..": "..v.Value)
        end
        return statData
    end)
end)

How can I return these values stored in 'UserStats' when a player joins? I was thinking of some string.sub and getting certain Ascii characters, but I'm not sure where to go on from there.

0
:GetAsync(key) is what you want, not :UpdateAsync() wazap 100 — 10y
0
He is correct with it. HexC3D 830 — 10y

1 answer

Log in to vote
0
Answered by
HexC3D 830 Moderation Voter
10 years ago
local datastore = game:GetService("DataStoreService"):GetDataStore("UserStats")

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady()
    local key = "user_" .. player.userId
  datastore:UpdateAsync(key, function(oldValue)
      local a = player:WaitForChild("leaderstats"):GetChildren()
 for i= 1,#a do
        DataStore:SetAsync(a[i].Name..": "..a[i].Value)
        end
      end)

This loading the script I changed the script to my liking and found it easier to do. Sorry for the changes but should work.

local datastore2 = game:GetService("DataStoreService"):GetDataStore("UserStats")
game.Players.PlayerAdded:connect(player)
wait(20)
player:WaitForDataReady()
  local key = "user_" .. player.userId
 if  DataStore:GetAsync(key) ~= nil then
local a2 = newplayer:WaitForChild("leaderstats"):GetChildren()
for i = 1, #a2 do
 datastore2:GetAsync(stats2[i].Name)
end
end
end)

This should work, hopefully.

~ Someone asks JSON I say "NO NOT AGAIN"

0
With that number of GetAsync in the for loop, wouldn't it flood the request limit per say when a lot of people join in a short interval of time? Dummiez 360 — 10y
0
Well you can prevent that by creating a script to not flood the request, may I edit to add it? HexC3D 830 — 10y
Ad

Answer this question