How do I save different datastores in one datastore?

I want to save different datastores in one datastore.

Currently I have four different datastores for saving data and if I add more I get a warning: “DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.” Another problem with multiple datastores is it lags when I stop testing in studio.

Script in ServerScriptService:

local DataStore = game:GetService("DataStoreService")
local LevelsDatastore = DataStore:GetDataStore("Levels003")
local GoldDataStore = DataStore:GetDataStore("Gold003")
local ExperienceDatastore = DataStore:GetDataStore("Exp003")
local ExperienceNeededDatastore = DataStore:GetDataStore("ExpNeed003")

game.Players.PlayerAdded:Connect(function(Plr)
	local statsFolder = Instance.new("Folder", Plr)
	statsFolder.Name = "Data"
	
	local leaderstatsFolder = Instance.new("Folder", Plr)
	leaderstatsFolder.Name = "leaderstats"

	local Levels = Instance.new("IntValue", leaderstatsFolder)
	Levels.Name = "Levels"
	Levels.Value = 1
	
	local Exp = Instance.new("IntValue", statsFolder)
	Exp.Name = "Exp"
	Exp.Value = 0
	
	local ExpNeed = Instance.new("IntValue", statsFolder)
	ExpNeed.Name = "ExpNeed"
	ExpNeed.Value = 200

	local Gold = Instance.new("IntValue", leaderstatsFolder)
	Gold.Name = "Gold"
	Gold.Value = 0

	Levels.Value = LevelsDatastore:GetAsync(Plr.UserId) or Levels.Value
	LevelsDatastore:SetAsync(Plr.UserId, Levels.Value)
	Levels.Changed:connect(function()
		LevelsDatastore:SetAsync(Plr.UserId, Levels.Value)
	end)

	Gold.Value = GoldDataStore:GetAsync(Plr.UserId) or Gold.Value
	GoldDataStore:SetAsync(Plr.UserId, Gold.Value)
	Gold.Changed:connect(function()
		GoldDataStore:SetAsync(Plr.UserId, Gold.Value)
	end)

	Exp.Value = ExperienceDatastore:GetAsync(Plr.UserId) or Exp.Value
	ExperienceDatastore:SetAsync(Plr.UserId, Exp.Value)
	Exp.Changed:connect(function()
		ExperienceDatastore:SetAsync(Plr.UserId, Exp.Value)
	end)

	ExpNeed.Value = ExperienceNeededDatastore:GetAsync(Plr.UserId) or ExpNeed.Value
	ExperienceNeededDatastore:SetAsync(Plr.UserId, ExpNeed.Value)
	ExpNeed.Changed:connect(function()
		ExperienceNeededDatastore:SetAsync(Plr.UserId, ExpNeed.Value)
	end)
end)

game.Players.PlayerAdded:Connect(function(plr)
	local DataFolder = plr:WaitForChild("Data")
	local leaderstatsFolder = plr:WaitForChild("Data")
	local Exp = DataFolder:WaitForChild("Exp")
	local Levels = leaderstatsFolder:WaitForChild("Levels")
	local ExpNeed = DataFolder:WaitForChild("ExpNeed")

	while wait() do
		if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then
			Levels.Value = Levels.Value + 1
			Exp.Value = Exp.Value - ExpNeed.Value
			ExpNeed.Value = ExpNeed.Value + 100
			game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(plr)
		end
	end
end)

game.Players.PlayerRemoving:connect(function(Player)
	LevelsDatastore:SetAsync(Player.UserId, Player.leaderstats.Levels.Value)
	GoldDataStore:SetAsync(Player.UserId, Player.leaderstats.Gold.Value)
	ExperienceDatastore:SetAsync(Player.UserId, Player.Data.Exp.Value)
	ExperienceNeededDatastore:SetAsync(Player.UserId, Player.Data.ExpNeed.Value)
end)

You could put all the values in a table and just save the table using only 1 datastore

1 Like

How would I load the table into different instance values?

accidentaly edited this bruh ((((((((((((((((((((((((((((

I did that but I get an “Unable to cast value to Object” error when every I use the SetAsync function

local DataStore = game:GetService("DataStoreService")

local EverythingDatastore = DataStore:GetDataStore("Everything003")

game.Players.PlayerAdded:Connect(function(Plr)
	local statsFolder = Instance.new("Folder", Plr)
	statsFolder.Name = "Data"
	
	local leaderstatsFolder = Instance.new("Folder", Plr)
	leaderstatsFolder.Name = "leaderstats"

	local Levels = Instance.new("IntValue", leaderstatsFolder)
	Levels.Name = "Levels"
	Levels.Value = 1
	
	local Exp = Instance.new("IntValue", statsFolder)
	Exp.Name = "Exp"
	Exp.Value = 0
	
	local ExpNeed = Instance.new("IntValue", statsFolder)
	ExpNeed.Name = "ExpNeed"
	ExpNeed.Value = 200

	local Gold = Instance.new("IntValue", leaderstatsFolder)
	Gold.Name = "Gold"
	Gold.Value = 0

	EverythingDatastore:SetAsync(Plr.UserId, Gold.Value, ExpNeed.Value, Levels.Value, Exp.Value)
	Levels.Changed:connect(function()
		EverythingDatastore:SetAsync(Plr.UserId, Gold.Value, ExpNeed.Value, Levels.Value, Exp.Value)
	end)
	Gold.Changed:connect(function()
		EverythingDatastore:SetAsync(Plr.UserId, Gold.Value, ExpNeed.Value, Levels.Value, Exp.Value)
	end)
	ExpNeed.Changed:connect(function()
		EverythingDatastore:SetAsync(Plr.UserId, Gold.Value, ExpNeed.Value, Levels.Value, Exp.Value)
	end)
	Exp.Changed:connect(function()
		EverythingDatastore:SetAsync(Plr.UserId, Gold.Value, ExpNeed.Value, Levels.Value, Exp.Value)
	end)
end)

game.Players.PlayerAdded:Connect(function(plr)
	local DataFolder = plr:WaitForChild("Data")
	local leaderstatsFolder = plr:WaitForChild("Data")
	local Exp = DataFolder:WaitForChild("Exp")
	local Levels = leaderstatsFolder:WaitForChild("Levels")
	local ExpNeed = DataFolder:WaitForChild("ExpNeed")

	while wait() do
		if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then
			Levels.Value = Levels.Value + 1
			Exp.Value = Exp.Value - ExpNeed.Value
			ExpNeed.Value = ExpNeed.Value + 100
			game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(plr)
		end
	end
end)

game.Players.PlayerRemoving:connect(function(Player)
	EverythingDatastore:SetAsync(Player.UserId, Player.leaderstats.Gold.Value, Player.Data.ExpNeed.Value, Player.leaderstats.Levels.Value, Player.Data.Exp.Value)
end)

You should save them like this EverythingDatastore:SetAsync(Plr.UserId,{Gold.Value,ExpNeed.Value,Levels.Value,Exp.Value})

Also you shouldn’t save them every time a value changes, maybe add a while loop (and on playerremoving)

Why should I not do that? (characterlimit)

Because it’s gonna save a lot of times and there will be a chance to get a warning. There is also a high chance that it will save on PlayerRemoving.

How would I load data?

With my current way the data just gets set to zero.
How I currently load data:

Levels.Value = EverythingDatastore:GetAsync(Plr.UserId) or Levels.Value
Gold.Value = EverythingDatastore:GetAsync(Plr.UserId) or Gold.Value
Exp.Value = EverythingDatastore:GetAsync(Plr.UserId) or Exp.Value
ExpNeed.Value = EverythingDatastore:GetAsync(Plr.UserId) or ExpNeed.Value

How I save data:

EverythingDatastore:SetAsync(Plr.UserId, {Gold.Value, ExpNeed.Value, Levels.Value, Exp.Value})

Nevermind I figured it out all I had to do is put [#] at the end of the getasyncfunction

Levels.Value = EverythingDatastore:GetAsync(Plr.UserId)[3] or Levels.Value
Gold.Value = EverythingDatastore:GetAsync(Plr.UserId)[1] or Gold.Value
Exp.Value = EverythingDatastore:GetAsync(Plr.UserId)[4] or Exp.Value
ExpNeed.Value = EverythingDatastore:GetAsync(Plr.UserId)[2] or ExpNeed.Value

Use a pcall() function

	local loadData = {}

	local success, fail = pcall(function()
		loadData = DataStore:GetAsync(plr.UserId.."-data")
	end)
	
	if success then
		Levels.Value = loadData[1]
        Gold.Value = loadData[2]
        Exp.Value = loadData[3]
        ExpNeed.Value = loadData[4]
    end