So I have a GlobalLeaderboard script that is supposed to order the player names according to the number of wins they have.
The script below is located in ServerScriptService.
My issue is that my output says that I have too many datastore requests and if more keep coming, the older ones get deleted.
How do I efficiently stop this?
local dataStoreService = game:GetService("DataStoreService") local players = game:GetService("Players") local globalDataStore = dataStoreService:GetOrderedDataStore("Wins") local board = workspace.Lobby.GlobalBoard local template = board.SurfaceGui.LeaderBoard.Template:Clone() board.SurfaceGui.LeaderBoard.Template:Destroy() local function update() for _,child in pairs(board.SurfaceGui.LeaderBoard:GetChildren()) do if child:IsA("Frame") then child:Destroy() end end local success,err = pcall(function() local data = globalDataStore:GetSortedAsync(false,40) local page = data:GetCurrentPage() for rank,plrData in ipairs(page) do local userid = plrData.key local wins = plrData.value if rank < 3 then local npc = workspace:FindFirstChild(rank) if npc then npc.UserId.Value = userid end end local new = template:Clone() new.PlrName.Text = players:GetNameFromUserIdAsync(userid) new.PlrAmount.Text = wins new.LayoutOrder = rank new.Parent = board.SurfaceGui.LeaderBoard end end) end while true do update() wait(math.random(2,5)) spawn(function() for _,plr in pairs(game.Players:GetPlayers()) do globalDataStore:SetAsync(plr.UserId,plr.leaderstats.Wins.Value) wait() end end) end
This just means you are updating the datastore too frequently.
In your while true do loop you use
wait(math.random(2,5))
But Set has a limit of 50 + (10*playerCount) per minute (https://developer.roblox.com/en-us/articles/Datastore-Errors).
You'd be best off achieving this by saving less frequently, i.e. every minute. The general advise on when to save is, when the player leaves (using Players.PlayerRemoving), when the game shutdowns (using BindToClose) and about every minute (using a loop like you have, but less frequently).
https://developer.roblox.com/en-us/api-reference/event/Players/PlayerRemoving https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose
If you really need to update that frequently, you might be able to get away with just saving when .Changed is fired on the value.