My game recently got featured on Roblox, and I’m a really worried about something. Currently, in any of the places except for 1 or 2, I’m getting “Exhausted DataStore” errors and I’m not entirely sure what to do to fix it. The data eventually loads but it takes it almost 10 minutes - something I’ve experienced only Roblox Studio.
local stuff={
"ClanTag","StringValue","";
"ClanTagPreview","StringValue","";
}
local DS=game:GetService"DataStoreService":GetDataStore("SoldierId","global")
game:GetService"Players".PlayerAdded:Connect(function(p)
local leaderstats=p:WaitForChild("SoldierId",.3)or table.foreach({''},function(i)i=Instance.new("Folder",p)i.Name="SoldierId"return(i)end)
for x=1,#stuff,3 do
local save;delay(3,function()save=true;end)
local val=Instance.new(stuff[x+1])
val.Name=stuff[x]
do local data=DS:GetAsync(p.UserId..stuff[x])
val.Value=data and type(stuff[x+2])==type(data)and data or stuff[x+2]end
val.Parent=leaderstats
val.Changed:Connect(function()if save then
save=false;DS:SetAsync(p.UserId..val.Name,val.Value)
delay(6,function()save=true;end)
end
end)
end
end)
It seems you are saving and loading data with the intent that it never fails. By this, I mean you aren’t checking if the datastores failed to save or load the user’s data. Because of this, the user’s data doesn’t load and when they leave, a blank data template is saved, or their data isn’t saved period. I also recommend checking to see if a user’s data has successfully been loaded before you attempt to save their data.
Loading the user’s data
Check in the save function so that we don’t save over a user that hasn’t loaded
And finally making sure our data saves if it passed the load test
The problem could be that you’re saving on Changed. Of course, you should have error handling and a retry system in place for when requests do fail, but you should also prevent issues where you can. I’m not sure how often Changed fires, but if players are able to change their ClanTag or ClanTagPreview rapidly, they will quickly exhaust your available requests (hence, the descriptive warning). In this case, you would need to only save it every couple of minutes, on PlayerRemoving, or when the game shuts down.
The reason why you are getting these warnings is because either:
You are trying to write (update/set/remove/increment) a key that was recently written to within 6 seconds.
You are out of budget.
This is global to the server. If one script tries to write a key and another tries to write the key too, one of them will throttle because you are violating the 6 second write rule. The same goes for budgets, budgets are shared for all scripts on a server.
To solve your issues, you need to make sure there is no way you can accidentally attempt writing a key again within 6 seconds, and that you can’t run out of budget. A simple way to do this is not to listen to changed-like signals / update whenever the player changes something, but instead save on a time interval and when players leave the server.