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

Why can't I save a table with more than 1 value in my datastore?

Asked by 6 years ago

I'm trying to save 2 values, strength and stamina. I want to save them both as a table in 1 datastore. I know this is possible, but it just isn't working. What am I doing wrong?

01-- Code that gives the player their saved stats when they join
02local players = game:GetService("Players")
03local DSService = game:GetService('DataStoreService'):GetDataStore('SecretTokenAboutAvatarwwww')
04 
05players.PlayerAdded:Connect(function(player)
06    local PlayerStats = Instance.new('Folder', player)
07    PlayerStats.Name = 'PlayerStats'
08    local str = Instance.new('IntValue', PlayerStats)
09    str.Name = 'Strength'
10    local stm = Instance.new('IntValue', PlayerStats)
11    stm.Name = 'Stamina'
12 
13    local uniquekey = "stats-"..player.userId
14    local GetSaved = DSService:GetAsync(uniquekey)
15    if GetSaved then
View all 24 lines...
01-- Code that saves their data when they leave
02local Players = game:GetService("Players")
03local DSService = game:GetService('DataStoreService'):GetDataStore('SecretTokenAboutAvatarwwww')
04 
05Players.PlayerRemoving:Connect(function(player)
06    local userId = "stats-"..player.userId
07    local data = {player.PlayerStats.Strength.Value,player.PlayerStats.Stamina.Value}
08    if data then
09        local success, result = pcall(function()
10            DSService:UpdateAsync(userId, data)
11        end)
12        if not success then
13            warn(result)
14        end
15    end
16end)

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

UpdateAsync()

UpdateAsync takes the 2nd parameter as a function, not a table nor value. then you'd return the new value. userId is deprecated, use UserId.

01-- the other stuff
02 
03local Players = game:GetService("Players")
04local DSService = game:GetService('DataStoreService'):GetDataStore('SecretTokenAboutAvatarwwww')
05 
06Players.PlayerRemoving:Connect(function(player)
07    local userId = "stats-"..player.UserId
08    local data = {player.PlayerStats.Strength.Value,player.PlayerStats.Stamina.Value}
09 
10    if data then
11        local success, result = pcall(function()
12    DSService:UpdateAsync(userId, function(oldvalues)
13        local newtable = data or oldvalues
14 
15        return newtable -- return the table
View all 22 lines...
0
Thanks a ton, I had no idea I had to return the value. repgainer3 35 — 6y
Ad

Answer this question