I have a datastore saving 20+ values. However, I keep getting this warning in the output. I need help optimizing this.
Warning: DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MainDataStore")
local function AddData(player)
local data = script.Data:Clone()
data.Parent = player
for _, Child in pairs(data:GetChildren()) do
Child.Parent = data.Parent
end
data:Destroy()
end
local function extractFromTable(Table, folder)
for _, Value in pairs(folder:GetChildren()) do
if Value:IsA("BoolValue") then
local ValueName = Value.Name
Value.Value = Table[ValueName] or false
elseif Value:IsA("IntValue") then
local ValueName = Value.Name
Value.Value = Table[ValueName] or 0
elseif Value:IsA("StringValue") then
local ValueName = Value.Name
if Value:GetAttribute("Ship") ~= nil then
Value.Value = Table[ValueName] or "Dmg: 1, Speed: 1"
else
Value.Value = Table[ValueName] or ""
end
elseif Value:IsA("Color3Value") then
local ValueName = Value.Name
local Color3Table = Table[ValueName]
if Color3Table then
Value.Value = Color3.new(Color3Table.R, Color3Table.G, Color3Table.B)
else
Value.Value = Color3.new(0, 0, 0) -- Default to white if not found in the table
end
end
end
end
local function insertIntoTable(Table, folder)
for _, Value in pairs(folder:GetChildren()) do
if Value:IsA("BoolValue") then
local ValueName = Value.Name
Table[ValueName] = Value.Value
elseif Value:IsA("IntValue") then
local ValueName = Value.Name
Table[ValueName] = Value.Value
elseif Value:IsA("StringValue") then
print(Value.Value)
local ValueName = Value.Name
Table[ValueName] = Value.Value
elseif Value:IsA("Color3Value") then
local colorTable = {
R = Value.Value.R,
B = Value.Value.B,
G = Value.Value.G
}
local ValueName = Value.Name
Table[ValueName] = colorTable
end
end
end
local function CreateData(player)
AddData(player)
local Currency = player:WaitForChild("leaderstats").Checks
local Ore = player:WaitForChild("OreFolder")
local Quests = player:WaitForChild("Quests")
local Shipstats = player:WaitForChild("Shipstats")
local shopFolder = player:WaitForChild("ShopFolder")
local Misc = player:WaitForChild("Misc")
local success, LoadedData = pcall(function()
return MyDataStore:GetAsync(tostring(player.UserId)) or {}
end)
if success then
Currency.Value = LoadedData.Coins or Currency.Value
local ShopColor = shopFolder:WaitForChild("ShopColor")
ShopColor.Value = Color3.new(LoadedData.R, LoadedData.G, LoadedData.B)
extractFromTable(LoadedData, Ore)
extractFromTable(LoadedData, Quests)
extractFromTable(LoadedData, Shipstats)
extractFromTable(LoadedData, shopFolder)
extractFromTable(LoadedData, Misc)
print("Data loaded successfully for player " .. player.Name)
else
warn("Failed to load data for player " .. player.Name)
end
end
function SaveData(player)
local Currency = player:WaitForChild("leaderstats").Checks
local Ore = player:WaitForChild("OreFolder")
local Quests = player:WaitForChild("Quests")
local Shipstats = player:WaitForChild("Shipstats")
local shopFolder = player:WaitForChild("ShopFolder")
local Misc = player:WaitForChild("Misc")
local dataToSave = {
Currency = Currency.Value,
}
insertIntoTable(dataToSave, Ore)
insertIntoTable(dataToSave, Quests)
insertIntoTable(dataToSave, Shipstats)
insertIntoTable(dataToSave, shopFolder)
insertIntoTable(dataToSave, Misc)
print(dataToSave)
local success, error = pcall(function()
MyDataStore:SetAsync(tostring(player.UserId), dataToSave)
end)
if success then
print("Data saved successfully for player " .. player.Name)
else
warn("Failed to save data for player " .. player.Name .. ": " .. error)
end
end
function Shutdown()
for _, player in pairs(game.Players:GetPlayers()) do
SaveData(player)
end
end
game.Players.PlayerAdded:Connect(CreateData)
game.Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(Shutdown)
while wait(300) do
for _, player in pairs(game.Players:GetPlayers()) do
SaveData(player)
end
end