Save all data in one datastore

Hello i need help , how to save all data in one datastore . is it okay to have alot of datastore?
what will happen?

local players = game:GetService("Players")
local dataStore = game:GetService("DataStoreService")
local CarDataStore1 = dataStore:GetDataStore("Motor1")
local CarDataSrore2 = dataStore:GetDataStore("Motor2")






players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "OwnedCars"
	folder.Parent = player
	
	
	local car1 = Instance.new("BoolValue")
	car1.Name = "HondeEX5"
	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 = "HondeWave100"
	car2.Parent = player.OwnedCars
	car2.Value = CarDataSrore2:GetAsync(player.UserId) or false
	CarDataSrore2:SetAsync(player.UserId, car2.Value)



	
	car1.Changed:Connect(function()
		CarDataStore1:SetAsync(player.UserId, car1.Value)
	end)

	car2.Changed:Connect(function()
		CarDataSrore2:SetAsync(player.UserId, car2.Value)
	end)


end)

i hope you guys can help me

You could store the data in a table, and then convert it into a string using HttpService:JSONEncode() and save it into the datastore. To get the table back, just get the datastore and HttpService:JSONDecode() it.

2 Likes

wow thanks for your advice , maybe i cant do that since im not a scripter [ still learning ].
is there another way to fix it? i still dont understand what to do with table

Crude sample, but this is what I normally do to save tables.

local datastoreservice = game:GetService("DataStoreService")
local httpservice = game:GetService("HttpService")
local playerDatastore = datastoreservice:GetDataStore("Data") -- or whatever datastore you want to save it to

local tabletosave = {
	["Money"] = 999,
	['Gems'] = 83,
	["Cars"] = {
		"Prius",
		"Mazda",
		"Toyota"
	}	
} -- Just an example of a table, it can be anything

local success, errormessage = pcall(function() -- to catch any errors
	-- since tables cannot be directly stored in a datastore, we convert it into a string and save it to the datastore
	playerDatastore:SetAsync("Data", httpservice:JSONEncode(tabletosave))
end)

if errormessage then warn(errormessage) end -- if it errors, it tells in the output with yellow text.
3 Likes

It is not a good idea to store all your data in one datastore. This makes it difficult to edit and takes memory.

if i do tons of datastore , everytime player join
the game kept sending me alot of warning of key request something like that which means not a good thing?

thanks for the script , i will try that

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