How do you make multiple saves in a data store

I sound really dumb but uhh

How do you add multiple saves in a datastore (look at example) and create tables?
Its not an ordered datastore

game.Players.PlayerAdded:connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local gems = Instance.new("IntValue", folder)
gems.Name = "Gems"
local Class = Instance.new("StringValue", folder)
Class.Name = "Class"
Class.Value = "Choosing"
end)





game.Players.PlayerAdded:Connect(function(plr)
	-- StatsDataStore
	local Ser = game:GetService("DataStoreService")
local Stoarge = Ser:GetDataStore('StatsDataStore')
	plr:WaitForChild("leaderstats")

		local key = plr.UserId
		plr.leaderstats.Gems.Value = Stoarge:GetASync(key, plr.leaderstats.Gems.Value)
		plr.leaderstats.Gem.value = Stoarge:GetAsync("-Class".. plr.UserId, plr.leaderstats.Class.Value)
		
	end)
	
	
	
	
	game.Players.PlayerRemoving:Connect(function(plr)
		local Ser = game:GetService("DataStoreService")
		local Storage = Ser:GetDataStore('StatsDataStore')
	   local work, errorn = pcall(function()

	 Storage:SetAsync(plr.UserId, plr.leaderstats.Gems.Value)
	
	 
	end)
		
	end)

What I have:

What I want it to look like[example]:

8 Likes

The Solution

Save your data in a table with special indexes to help make your data clearer.

How To Do It

Hiw to store data in a table like fashion:

--Saving
local data = {}
data.gems = plr.leaderstats.Gems.Value
data.coins = plr.leaderstats.Coins.Value
--You can add a ton of these
Storage:SetAsync(plr.UserId, data)

--Loading
local data = Storage:GetAsync(plr.UserId)
if data then
    plr.leaderstats.Gems.Value = data.gems
    plr.leaderstats.Coins.Value = data.coins
end

If you have any more questions, feel free to ask them.

Disclaimer: You should use pcalls with your datastore requests, I did not include them this time to make it easier to understand

32 Likes

I know for errors etc I am just improving on the code for now

Thanks.

Do you have any further questions? If you do not, glad I could help.

To answer your original question: You could store data on multiple keys, but you may experience throttling (Roblox limits the number of requests you can do). It is easier to store data in a table, as datastores have a max character limit of 260,000 characters, which will rarely be reached.

2 Likes

No This is perfect I got it to work :smile: ty

Instead of using 30 unneeded characters, hit the heart button. We understand. (:

3 Likes

Thanks this helps a lot :):):slight_smile:

1 Like

I’m glad you found it helpful, but in the future if you wish to say that you appreciate something, use the :heart: button to like the post. Otherwise, you end up giving notifications to everyone here (if they have them on), on a year old topic. If you have additional information to provide, making a new topic is a better solution.

Again, I’m glad you found this topic useful, but please don’t bump posts that are a year old.

5 Likes