Loading and saving multiple values at once in a datastore

  1. What do you want to achieve? I want to save and load multiple values into a datastore!

  2. What is the issue? If there are more than 4 values the others need a lot of time to load!

  3. What solutions have you tried so far? I tried to look for solutions on the devforum but I didn’t find anything to help me

Here is the script:

-- local players = game:GetService("Players")
local dataStore = game:GetService("DataStoreService")
local CarDataStore1 = dataStore:GetDataStore("VehicleDataStore0")

players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "OwnedCars"
	folder.Parent = player


	local car1 = Instance.new("BoolValue")
	car1.Name = "2020BloxWagenGulf"
	car1.Parent = player.OwnedCars
	car1.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car1.Value)
	
	local car2 = Instance.new("BoolValue")
	car2.Name = "1983PIATDOS"
	car2.Parent = player.OwnedCars
	car2.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car2.Value)
	
	local car3 = Instance.new("BoolValue")
	car3.Name = "2020TacoCarry"
	car3.Parent = player.OwnedCars
	car3.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car3.Value)
	
	local car4 = Instance.new("BoolValue")
	car4.Name = "2015PiatHundred"
	car4.Parent = player.OwnedCars
	car4.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car4.Value)
	
	local car5 = Instance.new("BoolValue")
	car5.Name = "2003JacuzziRS4"
	car5.Parent = player.OwnedCars
	car5.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car5.Value)
	
	local car6 = Instance.new("BoolValue")
	car6.Name = "2020TokenModelIII(Uncustomizable)"
	car6.Parent = player.OwnedCars
	car6.Value = CarDataStore1:GetAsync(player.UserId) or false
	CarDataStore1:SetAsync(player.UserId, car6.Value)
	
	---
	
	car1.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car1.Value)
	car2.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car2.Value)
	car3.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car3.Value)
	car4.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car4.Value)
	car5.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car5.Value)
	car6.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car6.Value)
						
end)
	end)
		end)
			end)
				end)
				end)
				end)
1 Like

Why don’t you format this all into a table, saving with tables can’t be that hard!
Just have every SetAsync second parameter be like car[1] or car[2] etc, and have the getAsync value be the same thing! You would have to format the table out first and some other things but if you know how use this to your advantage, I never save multiple individual values into one DS, I always use tables.

I’m not really experienced in scripting and I don’t have any idea of how a table really works

I don’t like this “Explanation”, Its a generally horrrible one that doesnt cover the groundwork of what they are or what they do, This wouldn’t help the OP at all as they may not even know what It is and may be confused on how to use it, If you wont explain what it is, I guess I will:

Tables (also known as Arrays or Dictionaries) are a Data Structuring method people use to represent Set of items, Records, Their Purpose is to store a Collection of Data.

An Array is a set of Values, or a Order list of values:

StoreShelf = {"Milk", "Apples", "Eggs", "OrangeJuice"} -- This is an Array

You are able to use table.insert to add to the Array or table.remove to remove from it.

table.insert(StoreShelf, "Butter") -- Adds "Butter" to the Array

table.remove(StoreShelf, "Butter")  -- Removes "Butter" from Array

You can find all these Methods here:

A Dictionary is a set of values with Keys in them, these Keys can Store values in them and can be named whatever:

Debugging = {
    ListFromResult = {},
    Attempts = 45,
    Successful = true,
    Errors = false,
    Output = "Hello world!"
} -- This is a Dictionary

To Add this Data, we have 2 Methods:

  1. Add it Directly into the table (which was shown above

  2. Add it by creating a Key

Which here is an Example:

Debugging.NewIndex = "hello" -- Adds index
-- Removing a Key
Debugging.NewIndex = nil -- Removes index

There is even whole Documentation about tables!

There are many methods for adding and removing Data from tables, But for this case we will be using Dictionaries for the DataStore


It is generally best for you to Save a table when using DataStoreService because all the Data is being Saved under one Request, the Reason your game is taking forever is because you are sending too many requests at a time which results in a Queue, You can only have a certain amount of times before this happens, once that happens it will:

  • Enter a Queue due to a Couple of Requests
    The Limits of the DataStore is reached causing the Request to line up in a queue

  • IF there are too many, The Request is dropped entirely
    Once there are too many requests at one time, the Data will be just completely dropped, giving you an error, It is best if you Handle your Data under one Key and under one Request, and if that request fails, you can always try again.

Here are a List of Error Codes

Sending 50 requests for a Single piece of Data is what causes issues to your DataStore, People Recommend Tables as that can be handled under 1 request, It can be used to Compact the Data together for Saving rather than saving them Individually.

local Array = {
    ["Level"] = 1
    ["Inventory"] = {}
}

CarDataStore1:SetAsync(player.UserId, Array) -- Saves all the Data in the Array

We also tend to wrap our code in a pcall().

What is a pcall()?

A pcall()(or Protected call) is an Error Handler used to Prevent Errors in your code, it is used to Check for errors within code or to Prevent them when doing specific task, this is included, DataStores when saving Data often Fail, so we need to Handle the Data under these pcalls to ensure they dont:

A pcall() as two Variables: The Success Variable, and the Error Variable, which is used to check for these things, but in this code, the Error Variable is used as both a error Variable and a Data Variable

local Success, Result = pcall(function() -- protected call
    return CarDataStore1:GetAsync(player.UserId) -- returns Data
end)

if Success then -- if the pcall was successful with no errors
    if Result then -- if there is Data
        -- handle your Data here
    else  -- if player has no Data

    end
else -- if there was an error

end

4 Likes

Thank you for the response! This helped me understand better tables and I will see if I can fix the problem!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.