Is there some way to modify this code to prevent the Data Store limits from being reached if too many players join at once?
function CreateValue(Name, Parent, Value) Value = Value or 0 local savevalue = Instance.new('IntValue', Parent) savevalue.Name = Name savevalue.Value = Value return savevalue end function GetValue(uniquekey) -- GetAsync local GetSaved = DSService:GetAsync(uniquekey) if GetSaved then return GetSaved end end game.Players.PlayerAdded:connect(function(plr) local uniquekey = ID..plr.userId local Levelss = Instance.new('IntValue', plr) Levelss.Name = leaderboardname for i,v in pairs(StatList) do local Stat = GetValue(i..uniquekey) if Stat then CreateValue(i, Levelss, Stat) --Old player load their value else CreateValue(i, Levelss, v) --New player load the default value end end end)
My DataStore script is pretty big, so when too many players join it fills up the requests and makes them wait a while for their data to load in. Is there any way to maybe make it so player's can manually load data? Or add some sort of wait to ensure that the limits aren't reached?
Thanks for reading
The reason why you will get data loss in this script is because of how you get the data. The way you are using the data store is incorrect and I am unsure what format StatList
is.
First you should be using a table when saving and loading data this will reduce the amount of requests to one to get or save player data. I cannot help you with how this should be formatted as you have not provided this information.
Second you are not safely catching any errors within the data store request and you need to wrap the data store request in a pcall
so you can retry if the first one failes. This includes the save process as well.
Lastly and I see this a lot with users data stores.
This means in most of the cases you only load the data when the user clicks a "play" button so that you only load data when it is needed. You would however need to keep track of who has loaded their data to know if it needs to be saved.
I cannot help you with making the code as I do not know the save or structure of your data.
hope this helps.
You could try implementing a loading queue, and yielding until the data is actually available. This wouldn't necessarily solve your problems as it might make players experience longer than average load times.
So the other thing you can do is try to reduce the amount of work you're trying to do. Like is there anything that isn't immediately essential that you can perhaps load later?
In addition if you save all player data at the same time in 5 minute intervals you'll have a sudden spike in datastore usage meaning if players join afterwards there will be less load/save availability. So you might want to try spreading that out as well.
Just a couple thoughts, I hope this helps. I'd be really curious to see what is causing you to use so many resources. Good luck!