Whenever I shut down my server using this code, I’m constantly getting the error “Datastore request was added to queue.” What is causing that?
I don’t think I was writing to the same key constantly, but I could be wrong.
--// Variables
local DataStoreService = game:GetService("DataStoreService")
local bombData = DataStoreService:GetDataStore("bombData")
local trailData = DataStoreService:GetDataStore("trailData")
local RunService = game:GetService("RunService")
local function setupPlayerData(player)
local PlayerBombInventory = Instance.new("Folder")
PlayerBombInventory.Name = "BombInventory"
PlayerBombInventory.Parent = player
local PlayerTrailInventory = Instance.new("Folder")
PlayerTrailInventory.Name = "TrailInventory"
PlayerTrailInventory.Parent = player
local success, data = pcall(function()
return bombData:GetAsync(player.UserId)
end)
if success then
if data then
for i,v in pairs(data) do
local BombValue = Instance.new("StringValue")
BombValue.Name = tostring(v.Name)
BombValue.Value = tostring(v.Value)
BombValue.Parent = PlayerBombInventory
end
else
warn("No previous data found; creating new data.")
end
end
local trailSuccess, trailData = pcall(function()
return trailData:GetAsync(player.UserId)
end)
if trailSuccess then
if trailData then
for i,v in pairs(trailData) do
local TrailValue = Instance.new("StringValue")
TrailValue.Name = tostring(v.Name)
TrailValue.Value = tostring(v.Value)
TrailValue.Parent = PlayerTrailInventory
end
else
warn("No previous data found; creating new data.")
end
end
end
local function saveOnExit(player)
local StoredBombs = {}
local StoredTrails = {}
for i, v in next, player.BombInventory:GetChildren() do
table.insert(StoredBombs, {Name = v.Name, Value = v.Value})
end
if StoredBombs then
local success, err = pcall(function()
bombData:SetAsync(player.UserId, StoredBombs)
end)
if not success then
warn("CANNOT SAVE DATA!")
end
end
for i, v in next, player.TrailInventory:GetChildren() do
table.insert(StoredTrails, {Name = v.Name, Value = v.Value})
end
if StoredTrails then
local trailSuccess, trailErr = pcall(function()
trailData:SetAsync(player.UserId, StoredTrails)
end)
if not trailSuccess then
warn("CANNOT SAVE DATA!")
end
end
end
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
coroutine.wrap(saveOnExit)(player)
end
end)
game.Players.PlayerAdded:Connect(setupPlayerData)
game.Players.PlayerRemoving:Connect(saveOnExit)