DataStore request was added to queue | DataStore2

  1. What do you want to achieve?

I want to save data without queuing it because there may be an error with saving data for the player and I don’t want that

  1. What is the issue?

I have a problem with the database, because I know that it can be done better, I use the DataStore2 module and there is an overflow error, I am looking for a way to fix this error and does anyone who has experience with databases have any ideas on how to fix it?

 16:18:59.346  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 127  -  Studio
  16:18:59.745  saved PlayerData  -  Server - DataStore2:266
  16:18:59.745  Data store PlayerData was not saved as it was not updated.  -  Server - DataStore2:280
  16:18:59.746  saved PlayerData  -  Server - DataStore2:266
  16:18:59.748  Data store PlayerData was not saved as it was not updated.  -  Server - DataStore2:280
  16:18:59.748  saved PlayerData  -  Server - DataStore2:266
  16:18:59.753  Data store PlayerData was not saved as it was not updated.  -  Server - DataStore2:280
  16:18:59.753  saved PlayerData  -  Server - DataStore2:266
  16:18:59.757  Data store PlayerData was not saved as it was not updated.  -  Server - DataStore2:280
  16:18:59.757  saved PlayerData  -  Server - DataStore2:266
  16:18:59.761  Data store PlayerData was not saved as it was not updated.  -  Server - DataStore2:280
  16:18:59.761  saved PlayerData  -  Server - DataStore2:266

CODE:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local DataStore2 = require(game.ServerScriptService:WaitForChild("DataStore2"))
DataStore2.Combine("PlayerData", "money", "level", "xp", "rockSpawn", "biome", "rocks")

local ROCK_CONFIG = {
	["StoneRock"] = {unlockedByDefault = true},
	["IronOre"] = {unlockedByDefault = false},
	["GoldOre"] = {unlockedByDefault = false},
	["EmberCoal"] = {unlockedByDefault = false},
	["Solgold Vein"] = {unlockedByDefault = false},
	["Grimshard"] = {unlockedByDefault = false}
}

local function getDefaultRocks()
	local rocks = {}
	for rock, conf in pairs(ROCK_CONFIG) do
		rocks[rock] = conf.unlockedByDefault
	end
	return rocks
end

local DEBOUNCE_TIME = 5
local lastSaveTime = 0

local function safeSave(store, data)
	local now = os.time()
	if now - lastSaveTime >= DEBOUNCE_TIME then
		lastSaveTime = now
		store:Set(data)
	end
end

local function setupFolders(player)
	local playerStore = DataStore2("PlayerData", player)
	local defaultData = {
		money = 0,
		level = 1,
		xp = 0,
		rockSpawn = 5,
		biome = "Grassland",
		rocks = getDefaultRocks()
	}
	local playerData = playerStore:Get(defaultData)

	-- leaderstats
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = playerData.money
	money.Parent = leaderstats

	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value = playerData.level
	level.Parent = leaderstats

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

	local xp = Instance.new("IntValue")
	xp.Name = "XP"
	xp.Value = playerData.xp
	xp.Parent = stats

	-- PlotSettings (RockSpawn, Biome)
	local plot = Instance.new("Folder")
	plot.Name = "PlotSettings"
	plot.Parent = player

	local rockSpawn = Instance.new("IntValue")
	rockSpawn.Name = "RockSpawn"
	rockSpawn.Value = playerData.rockSpawn
	rockSpawn.Parent = plot

	local biome = Instance.new("StringValue")
	biome.Name = "Biome"
	biome.Value = playerData.biome
	biome.Parent = plot

	-- Unlocks (Rocks)
	local unlocks = Instance.new("Folder")
	unlocks.Name = "Unlocks"
	unlocks.Parent = player

	for rockName, unlocked in pairs(playerData.rocks) do
		local bool = Instance.new("BoolValue")
		bool.Name = rockName
		bool.Value = unlocked
		bool.Parent = unlocks
	end

	playerStore:OnUpdate(function(newData)
		money.Value = newData.money
		level.Value = newData.level

		xp.Value = newData.xp

		rockSpawn.Value = newData.rockSpawn
		biome.Value = newData.biome

		for rockName, unlocked in pairs(newData.rocks) do
			local bool = unlocks:FindFirstChild(rockName)
			if bool then
				bool.Value = unlocked
			end
		end
	end)

	local function connectValueChanged(value, key)
		value.Changed:Connect(function(newValue)
			if playerData[key] ~= newValue then
				playerData[key] = newValue
				safeSave(playerStore, playerData)
			end
		end)
	end

	connectValueChanged(money, "money")
	connectValueChanged(level, "level")
	connectValueChanged(xp, "xp")
	connectValueChanged(rockSpawn, "rockSpawn")
	connectValueChanged(biome, "biome")

	for _, bool in ipairs(unlocks:GetChildren()) do
		if bool:IsA("BoolValue") then
			bool.Changed:Connect(function(newValue)
				playerData.rocks[bool.Name] = newValue
				safeSave(playerStore, playerData)
			end)
		end
	end

	player.AncestryChanged:Connect(function(_, parent)
		if not parent then
			playerStore:Set(playerData)
		end
	end)
end

local function unlockRock(player, rockName)
	if not ROCK_CONFIG[rockName] then return false end

	local rocksStore = DataStore2("rocks", player)
	local rocks = rocksStore:Get(getDefaultRocks())

	if not rocks[rockName] then
		rocks[rockName] = true
		player.Unlocks[rockName].Value = true
		rocksStore:Set(rocks)
		return true
	end
	return false
end

local function isRockUnlocked(player, rockName)
	local rocksStore = DataStore2("rocks", player)
	local rocks = rocksStore:Get(getDefaultRocks())
	return rocks[rockName] or false
end

Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		DataStore2("money", player):Save()
		DataStore2("level", player):Save()
		DataStore2("xp", player):Save()
		DataStore2("rockSpawn", player):Save()
		DataStore2("biome", player):Save()
		DataStore2("rocks", player):Save()
	end)
end)

Players.PlayerAdded:Connect(setupFolders)

game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		pcall(function()
			DataStore2("money", player):Save()
			DataStore2("level", player):Save()
			DataStore2("xp", player):Save()
			DataStore2("rockSpawn", player):Save()
			DataStore2("biome", player):Save()
			DataStore2("rocks", player):Save()
		end)
	end
end)

I would be grateful for any help

I don’t know much about Datastore2 cause i’ve only used it for a short time, but in my experience Datastore2 automatically saves all player’s datastores when they leave so you don’t have to write Datastore2("Your data", player):Save() whenever they leave as it is already handled by Datastore2.

I used to get the exact same warnings as you because I was used to saving data everytime someone leaves the game inside Players.PlayerRemoving with the regular DataStore service, but then i removed all the Datastore2(data, player):Save() lines and the warnings stopped, probably because they where already saved by Datastore2 automatically

I hope this helps.

1 Like

Yeah, I had the exact same habit — I was so used to manually saving data when a player left that I kept doing it with Datastore2 too. What you said actually helped a lot, I didn’t realize it saves automatically. I’ll definitely remember that for next time. Thanks for clearing it up

1 Like

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