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

How to save multiple values in a datastore?

Asked by
FiredDusk 1466 Moderation Voter
7 years ago
Edited 7 years ago

This code is suppose to save both values but not even saving one. I get no error.

local DS = game:GetService('DataStoreService')
local Data = DS:GetDataStore('Data0')

game.Players.PlayerAdded:connect(function(player)
    local folder = Instance.new('Folder',player)
    folder.Name = 'Leaderstats'
    local credits = Instance.new('IntValue',folder)
    credits.Name = 'Credits'
    local lapAmmount = Instance.new('IntValue',folder)
    lapAmmount.Name = 'LapAmmount'

    credits.Value = Data:GetAsync(player.UserId) or 0
    lapAmmount.Value = Data:GetAsync(player.UserId) or 1

    Data:SetAsync(player.UserId, {
        credits.Value,
        lapAmmount.Value        
    })
end)

game.Players.PlayerRemoving:connect(function(player)
    Data:SetAsync(player.UserId, {
        player.Leaderstats.Credits.Value,
        player.Leaderstats.LapAmmount.Value 
    })
end)
0
Use a table? OldPalHappy 1477 — 7y
0
I edited the code. FiredDusk 1466 — 7y
0
You do not load the data correctly and I would not save data using the changed event as you will max the key limit. User#5423 17 — 7y
0
How would I load the data correctly then? I am very new to datastores. FiredDusk 1466 — 7y

1 answer

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

In a data store we save data as a table to save on the number of requests needed to load / save the players data due to a limited request count.

This table is saved in a JSON format then saved (this is all managed by roblox) and then decoded giving us back the original table of data we saved. We cannot save a mixed table ie a table with both an array part and a key value part as this is not compatible with the JSON format.

To access data we use its key to get the associated data which will be nil if there is no data or the data saved. In most cases we want the key to be unique so we simply add the UserId, I would recommend that you include a prefix such as usr_ ect as you may want to use more than one key for that user.

Putting it all together:-

local Data = game:GetService('DataStoreService'):GetDataStore('Data0')

local prefix = 'user_' -- example prefix


game.Players.PlayerAdded:connect(function(player)
    local folder = Instance.new('Folder')
    folder.Name = 'Leaderstats'

    local credits = Instance.new('IntValue',folder)
    credits.Name = 'Credits'

    local lapAmmount = Instance.new('IntValue',folder)
    lapAmmount.Name = 'LapAmmount'

    local plrData = Data:GetAsync(prefix  .. tostring(player.UserId))

    if plrData then
          credits.Value = plrData[1] or 0
          lapAmmount.Value = plrData[2] or 0
   else
        -- most people like to save the data if they have none 

    Data:SetAsync(prefix  .. tostring(player.UserId), {
        credits.Value,
        lapAmmount.Value        
     })
   end

end)

game.Players.PlayerRemoving:connect(function(player)
    Data:SetAsync(prefix  .. tostring(player.UserId), {
        player.Leaderstats.Credits.Value,
        player.Leaderstats.LapAmmount.Value 
    })
end)

This is a very basic example with duplicate code, I hope this helps.

0
Nothing saved. FiredDusk 1466 — 7y
0
nvm, thanks. FiredDusk 1466 — 7y
Ad

Answer this question