You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want to make my save data script more efficient so that it saves the player’s leaderstats with fewer requests. -
What is the issue? Include screenshots / videos if possible!
I keep getting an error message in testing if I change my stats too fast:
“DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.”
This is with one player in Studio testing, so it is probably even worse with a full server and stats changing every few seconds…
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I went through multiple tutorials and was able to create a leaderboard/crafting script that works, but sends a datastore request every time a player’s stat value changes. I am pretty new to scripting, and I know it is inefficient, but I am unsure how to improve it. In my game, there are four different resources as leaderstats: “Wood”, “Stone”, “Iron”, and “Explorite”, and all are changed frequently as the player uses their tools to mine the resources scattered around the map. I also have a tool-crafting section of script at the bottom, as in my game you can use resources to craft tools. When a player presses a TextButton in the GUI while having enough resources, the tool gets crafted and the required resources are taken.
This is my current script (a regular Script in ServerScriptService). I also have more of the crafting stuff in other places: crafting recipes ModuleScript, a RemoteFunction, and a folder of all my tools all in ReplicatedStorage. The crafting system works great, but the materials data save part is really inefficient.
-----------------------Variables/Setup--------------------------------
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tools = ReplicatedStorage:WaitForChild("Tools")
local CraftTool = ReplicatedStorage:WaitForChild("CraftTool")
local craftingInfo = require(ReplicatedStorage:WaitForChild("CraftingInfo"))
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("WoodSaveSystem")
local ds2 = datastore:GetDataStore("StoneSaveSystem")
local ds3 = datastore:GetDataStore("IronSaveSystem")
local ds5 = datastore:GetDataStore("ExploriteSaveSystem")
game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-------------------Data Saving System-----------------------------
local Wood = Instance.new("IntValue", leaderstats)
Wood.Name = "Wood"
local Stone = Instance.new("IntValue", leaderstats)
Stone.Name = "Stone"
local Iron = Instance.new("IntValue", leaderstats)
Iron.Name = "Iron"
local Explorite = Instance.new("IntValue", leaderstats)
Explorite.Name = "Explorite"
-- data saves
Wood.Value = ds1:GetAsync(player.UserId) or 10
ds1:SetAsync(player.UserId, Wood.Value)
Stone.Value = ds2:GetAsync(player.UserId) or 0
ds2:SetAsync(player.UserId, Stone.Value)
Iron.Value = ds3:GetAsync(player.UserId) or 0
ds3:SetAsync(player.UserId, Iron.Value)
Explorite.Value = ds5:GetAsync(player.UserId) or 0
ds5:SetAsync(player.UserId, Explorite.Value)
-- update data saves
Wood.Changed:connect(function()
ds1:SetAsync(player.UserId, Wood.Value)
end)
Stone.Changed:connect(function()
ds2:SetAsync(player.UserId, Stone.Value)
end)
Iron.Changed:connect(function()
ds3:SetAsync(player.UserId, Iron.Value)
end)
Explorite.Changed:connect(function()
ds5:SetAsync(player.UserId, Explorite.Value)
end)
--
end)
-------------------Crafting System----------------------
--function
CraftTool.OnServerInvoke = function(player, toolName)
local leaderstats = player.leaderstats
--materials
local Stone = leaderstats.Stone
local Wood = leaderstats.Wood
local Iron = leaderstats.Iron
local Explorite = leaderstats.Explorite
--crafted?
local crafted = false
--craft the tool--
for i, v in pairs (craftingInfo[toolName]) do
local material = leaderstats:FindFirstChild(i)
if material.Value >= v then
--tool into player backpack
material.Value = material.Value - v
local tool = Tools:FindFirstChild(toolName):Clone()
tool.Parent = player.Backpack
local startertool = Tools:FindFirstChild(toolName):Clone()
startertool.Parent = player.StarterGear
print ("toolcrafted")
else
--not enough
print ("not enough")
end
end
end
I can post the other scripts if necessary, but I hope this works! Thank you!