How do you make multiple saves in a data store

#1

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]:

1 Like
#2

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

7 Likes
#3

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

Thanks.

#4

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.

#5

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

#6

If you found my post as a solution, make sure to mark it as one so others can find it easily.

If you didn’t, that’s fine: I can help you solve the problem.

#8

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

1 Like