Getting multiple values from a datastore?

Very rookie mistake but this is my first time using datastores and I wanted to get multiple values from a datastore. I’m setting the datastore like this:

currencyDataStore:SetAsync(player.UserId.."-currency", player.leaderstats.Kills.Value, player.leaderstats["Dream Tokens"].Value)

and getting it like this:

local data = currencyDataStore:GetAsync(player.UserId.."-currency")
	
kills.Value = data[0] -- lines with errror
kills.Value = data[1]

I get this error: ServerScriptService.CurrencyData:29: attempt to index nil with number

I know I’m doing something wrong but I can’t find any good fixes so umm, please comment if you know. Thanks!

Indexes in roblox start at 1, not 0, so you’ll have to do data[1] and data[2]

Hi! Yes, pretty close. Best to put the stuff you want to save in a table, and save the table!

Best to make sure the keys are all strings, and ofc that the values in the table are strings/numbers/bools etc, in any case not object references (because data stores won’t be able to save them).

plrData = {
  kills = player.leaderstats.Kills.Value,
  tokens = player.leaderstats["Dream Tokens"].Value,
}
currencyDataStore:SetAsync(player.UserId.."-currency", plrData)

Giving a name to each saved item is better than a number. Also because if a number is missing it breaks the save. Be sure to wrap it all in a pcall, because datastore commands can sometimes fail! :slight_smile:

1 Like

This actually really helped but how would i get the list of data? If i get async, will it retun the table?

It says attempt to index nil with number, andwhat this line returns can be nil. Consider checking if the data exists and creating starting data.

1 Like

Yep! As @myaltaccountsthis points out:

local function getDefaultData()
  return {kills = 0, tokens = 0,}
end

local success, errorMessage = pcall(function()
  return currencyDataStore:GetAsync(player.UserId.."-currency") 
end)

if success then
  data = errorMessage or getDefaultData()

  print("My kills: ", data["kills"])
else
  warn("Something went wrong, data could not be loaded: ", errorMessage)
  --retry logic
end

I didn’t test this code so there may be typos, but something like this can be built around it to make sure things don’t crash before your first save, while not overwriting your old save in case of datastore failure. Most people write a more complicated ‘playerstatmanager’ around it, with some retry logic and/or other ways to deal with errors in the async call.

Good luck!

1 Like

Amazing! Its real late right now so i cant’t check but I’ll il. Give the solution as soon as i can. Thanks!

Just to clear a bit of confusion, Is the table returned to sucess or errormsg?

1 Like

If you use pcall, and return a value in the function in there, it is passed as the second return value from pcall. Thats because the first return value is already a boolean describing wether the pcall failed or succeeded (so in that case success == ‘true’).

If the pcall fails, then the first return argument from pcall is false, and the second will be the errormessage. Thats why you could equally have named it:

local success, data = pcall(etc) -- same function in here as before

if not success then 
  local errorMessage = data
  warn(errorMessage)
end

It of course does the same thing, but it could be considered more clear.

1 Like

Thanks for clearing that up! Ill give you the solution as soon as i can.

1 Like