I have a data store that works most of the time, but every once in a while I get the error: “Data store request was added to queue.” I feel like players are telling me they lose their data too often, so I want to find a way to solve this issue.
This code is a little old, and I have tried changing the auto save interval many times. I have looked around other dev forum data store posts, but I still can’t figure out what the issue is. The code is posted below, thanks for looking! Also, this is my first dev forum post.
-- 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 = 200
-- Function that other scripts can call to change a player's stats
-- For table changing, the value will be a table. The first
-- part of the table will be the index, the second will be the value (1)
-- StatName is a string
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]) == "table" then
sessionData[playerUserId][statName][value[1]] = value[2]
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
-- Checking if player has weapons in their data, because I added it as an update
if not sessionData[playerUserId]["Weapons"] then
sessionData[playerUserId]["Weapons"] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
end
else
-- Data store is working, but no current data for this player
sessionData[playerUserId] = {GBucks=0,HitCash=0,IdleEarnings=0,InjuryMultiplier=0,Ragdolls={0,0,0,0,0,0,0,0,0},HitEffects={0,0,0,0,0,0,0,0},Trails={0,0,0,0,0,0,0,0},Weapons={0,0,0,0,0,0,0,0,0,0,0,0,0,0,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
if not success then
warn("Cannot save data for player!")
end
end
end
-- 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
Edit: Also important to note I use the “ChangeStat” function VERY frequently, which I don’t think should be causing the issue but I am not completely sure. I also noticed that every player’s data saves at once, and I am wondering what is the optimal data save wait period.