so i made a data store with autosave and some more things but when im on studio it giving me this warning DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = x and my studio crashes
here is my code
local datastoreService = game:GetService("DataStoreService")
local Datastore = datastoreService:GetDataStore("SomeRandomDataStoreDude")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Parent = leaderstats
coins.Name = "Coins"
coins.Value = 0
local data
local success, errormessage = pcall(function()
data = Datastore:GetAsync(player.UserId)
end)
if success then
coins.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {
player.leaderstats.Coins.Value
}
for i,v in pairs(data) do
local success, errormessage = pcall(function()
Datastore:SetAsync(player.UserId, v)
print("hi")
end)
if success then
print("Data is saved")
else
print("theres an error")
warn(errormessage)
end
end
end)
game:BindToClose(function()
if RunService:IsStudio() then
wait(1)
end
for i,player in ipairs(game.Players:GetPlayers()) do
local data = {
player.leaderstats.Coins.Value
}
for i,v in ipairs(data) do
local success, errormessage = pcall(function()
Datastore:SetAsync(player.UserId, v)
print("hi")
end)
if success then
print("Data is saved")
else
print("theres an error")
warn(errormessage)
end
end
end
end)
while true do
wait(15)
for i,player in ipairs(game.Players:GetPlayers()) do
local data = {
player.leaderstats.Coins.Value
}
for i,v in ipairs(data) do
local success, errormessage = pcall(function()
Datastore:SetAsync(player.UserId, v)
print("hi")
end)
if success then
print("Data is saved")
else
print("theres an error")
warn(errormessage)
end
end
end
end
how can i make the warning stop? is my data store good? how can i improve it?
thanks
Instead of saving data on player leave and bind to close, save them when the datastore changes. Use ARandomDataStore:OnUpdate() to save data only when a player levels up, etc.
Then, make a function that saves data only the end of per round? If you tell me more about the game concept, I will be able to help you determine when and how to save your code.
Okay, then save data when the player sells stuff, they level up, purchase a gamepass, or a dev product. You may also update all the datastores in a 300 second cooldown automatically.
Why exactly do you need an Autosave? Saving when the player leaves or the server shutdowns is already good enough
If you still want an autosave, you’d have to make it save at long intervals, like 15 minutes or higher to not get the warning, but at that point, depending o nyour game, you’re really better of just making it save on leave or server shutdown
Make a variable for getting the HTTPService and call the variable HTTPService and change each
local data = {
player.leaderstats.Coins.Value
}
for i,v in ipairs(data) do
local success, errormessage = pcall(function()
Datastore:SetAsync(player.UserId, v)
print("hi")
end)
if success then
print("Data is saved")
else
print("theres an error")
warn(errormessage)
end
end
To
local tbl = {
["Coins"] = player.leaderstats.Coins.Value
}
lcoal data = HTTPService:JSONEncode(tbl)
local success, errormessage = pcall(function()
Datastore:SetAsync(player.UserId, data)
print("hi")
end)
if success then
print("Data is saved")
else
print("theres an error")
warn(errormessage)
end
And then to retrieve the data back, just use JSONDecode and reference the Coins key
The main problem here is the server is closing when you are leaving the game, so its saving the data twice (with bindtoclose and playerleave) very fast, hence why you are getting this error.
What you could do is listen when the ancestry of the player change, if the parent is nil, that means they left the game. And you create a function in bind to close which set the parent of every player to nil
now its not giving me the error, but its not saving
here is the new code
local datastoreService = game:GetService("DataStoreService")
local Datastore = datastoreService:GetDataStore("SomeRandomDataStoreDude")
local RunService = game:GetService("RunService")
local HttpService = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("StringValue")
Coins.Parent = leaderstats
Coins.Name = "Coins"
Coins.Value = 0
local data
local success, errormessage = pcall(function()
data = Datastore:GetAsync(player.UserId)
end)
if success then
Coins.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
local tbl = {
["Coins"] = player.leaderstats.Coins.Value
}
local data = HttpService:JSONEncode(tbl)
Datastore:SetAsync(player.UserId, data)
end)
if not success then
print("theres an error")
warn(errormessage)
end
end)
game:BindToClose(function()
if RunService:IsStudio() then
wait(1)
end
for i, player in pairs(game.Players:GetPlayers()) do
local success, errormessage = pcall(function()
local tbl = {
["Coins"] = player.leaderstats.Coins.Value
}
local data = HttpService:JSONEncode(tbl)
Datastore:SetAsync(player.UserId, data)
end)
if not success then
print("theres an error")
warn(errormessage)
end
end
end)
``` did i do something wrong?
local data
local success, errormessage = pcall(function()
data = HttpService:JSONDecode(Datastore:GetAsync(player.UserId))
end)
if success then
Coins.Value = data["Coins"]
end