I need help with a datastore

Hello I was wondering if anyone could help me with a datastore problem. So I keep getting the same warning/error:

DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 181542590

I was wondering if it was trying to save too much data at once, but then again I was only testing one “leaderstat value”.

The actual script:
– Set up table to return to any script that requires this module script
local PlayerStatManager = {}

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

-- Table to hold player information for the current session
local sessionData = {}

local AUTOSAVE_INTERVAL = 60

-- Function that other scripts can call to change a player's stats
    function PlayerStatManager:ChangeStat(player, statName, value)
	local playerUserId = "Player_" .. player.UserId
	assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error:              types do not match")
	if typeof(sessionData[playerUserId][statName]) == "number" then
		sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
	else
		sessionData[playerUserId][statName] = value
	end
    end

-- Function to add player to the "sessionData" table
local function setupPlayerData(player)
	local playerUserId = "Player_" .. player.UserId
	local success, data = pcall(function()
		return playerData:GetAsync(playerUserId)
	end)
	if success then
		if data then
			-- Data exists for this player
			sessionData[playerUserId] = data
		else
			-- Data store is working, but no current data for this player
			sessionData[playerUserId] = {Money=0, Experience=0}
		end
	else
		warn("Cannot access data store for player!")
	end
    end

-- Function to save player's data
local function savePlayerData(playerUserId)
	if sessionData[playerUserId] then
		local tries = 0	
		local success
		repeat
			tries = tries + 1
			success = pcall(function()
				playerData:SetAsync(playerUserId, sessionData[playerUserId])
			end)
			if not success then wait(1) end
		until tries == 3 or success
		-- Function to save player data on exit
		local function saveOnExit(player)
			local playerUserId = "Player_" .. player.UserId
			savePlayerData(playerUserId)
		end

		-- Function to periodically save player data
		local function autoSave()
			while wait(AUTOSAVE_INTERVAL) do
				for playerUserId, data in pairs(sessionData) do
					savePlayerData(playerUserId)
				end
			end
		end

		-- Start running "autoSave()" function in the background
		spawn(autoSave)

		-- Connect "setupPlayerData()" function to "PlayerAdded" event
		game.Players.PlayerAdded:Connect(setupPlayerData)

		-- Connect "saveOnExit()" function to "PlayerRemoving" event
		game.Players.PlayerRemoving:Connect(saveOnExit)

		return PlayerStatManager

This datastore was used from this topic here

All Help Is Appreciated, Thank You :slightly_smiling_face:

It looks like every time you call savePlayerData it runs these lines of code as well:

local function saveOnExit(player)
		local playerUserId = "Player_" .. player.UserId
		savePlayerData(playerUserId)
end
game.Players.PlayerRemoving:Connect(saveOnExit)

since the function saveOnExit is in the function savePlayerData, maybe this could cause? I’m not 100% however. If you’ve got any other scripts that handle stuff with datastore, please show them here if possible

I do have another datastore script that I will use as a backup but it shows the same message/error

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.Clicks
	local save2 = plr.leaderstats.Gems
	local save3 = plr.leaderstats.Rebirths
	local save4 = plr.leaderstats.Eggs
	
	local getSaved = ds:GetAsync(plrkey)
	if getSaved then
		save1.Value = getSaved[1]
		save2.Value = getSaved[2]
		save3.Value = getSaved[3]
		save4.Value = getSaved[4]
	else
		local numberForSaving = {save1.Value, save2.Value, save3.Value, save4.Value}
		ds:GetAsync(plrkey, numberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, {plr.leaderstats.Clicks.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Rebirths.Value, plr.leaderstats.Eggs.Value})
end)

I don’t see anything that could be causing that error in this script, maybe you could try to fix this possible issue:

My theory is that it’s calling itself every time, which could be repeating the repeat loop that saves player data, thus spamming data store requests?

Ok, I will try this and let you know what happens. In the mean time thank you for your support! :smiley:

Hello, So I tried your method and it gave me the same output.