Right now, I’m currently working on a leaderboard system for a game I’m working on and I’m having some issues with throttling that I’m a bit confused about. I’m aware of the functions I’m using that may cause this (GetAsync - Loading value data - GetSortedAsync - Load value data for leaderboards - SetAsync - For saving players values)
For a while, there is no throttling but it eventually starts throttling out of the blue and it’s confusing me quite heavily.
I’ll post the scripts, I’d like some feedback on ways I could improve on them to try and reduce throttling, I’m very new to OrderedDataStores.
LeaderBoardDataHandler: This handles the data for each of the values that are being saved for the leaderboard to use.
This script is in ServerScriptService.
local DSService = game:GetService("DataStoreService")
local totalMoneyMade = DSService:GetOrderedDataStore("TotalMoneyMade")
local totalNpcsServed = DSService:GetOrderedDataStore("TotalNpcsServed")
local totalMissionsCompleted = DSService:GetOrderedDataStore("TotalMissionsCompleted")
local totalAlienDamage = DSService:GetOrderedDataStore("TotalAlienDamage")
local updateLeaderData = script.Parent.UpdateLeaderData
local updateLeaderDataEvent = game.ReplicatedStorage.UpdateLeaderData
game.Players.PlayerAdded:Connect(function(plr)
wait(1)
local totalMoney = totalMoneyMade:GetAsync(plr.userId) or 0
local totalNpcsServedPage = totalNpcsServed:GetAsync(plr.userId) or 0
local totalMissionsCompletedPage = totalMissionsCompleted:GetAsync(plr.userId) or 0
local totalAlienDamagePage = totalAlienDamage:GetAsync(plr.userId) or 0
plr:WaitForChild("LeaderBoardStats").TotalAlienDamage.Value = totalAlienDamagePage
plr:WaitForChild("LeaderBoardStats").MissionsCompleted.Value = totalMissionsCompletedPage
plr:WaitForChild("LeaderBoardStats").TotalMoneyMade.Value = totalMoney
plr:WaitForChild("LeaderBoardStats").NpcsServed.Value = totalNpcsServedPage
end)
game.Players.PlayerRemoving:Connect(function(plr)
local function updateAsync()
totalMoneyMade:UpdateAsync(plr.userId, function(oldValue)
local newValue = oldValue or 0
newValue = plr:WaitForChild("LeaderBoardStats").TotalMoneyMade.Value
return newValue
end)
totalNpcsServed:UpdateAsync(plr.userId, function(oldValue)
local newValue = oldValue or 0
newValue = plr:WaitForChild("LeaderBoardStats").NpcsServed.Value
return newValue
end)
totalMissionsCompleted:UpdateAsync(plr.userId, function(oldValue)
local newValue = oldValue or 0
newValue = plr:WaitForChild("LeaderBoardStats").MissionsCompleted.Value
return newValue
end)
totalAlienDamage:UpdateAsync(plr.userId, function(oldValue)
local newValue = oldValue or 0
newValue = plr:WaitForChild("LeaderBoardStats").TotalAlienDamage.Value
return newValue
end)
end
local success, err = pcall(updateAsync)
if success then
print("Cool beans")
else
print("No, does not work: "..err.."")
end
end)
updateLeaderDataEvent.OnServerEvent:connect(function(plr)
local function updateAsync()
totalMoneyMade:UpdateAsync(plr.userId, function(oldValue)
local newValue = oldValue or 0
newValue = plr:WaitForChild("LeaderBoardStats").TotalMoneyMade.Value
return newValue
end)
totalNpcsServed:UpdateAsync(plr.userId, function(oldValue)
local newValue = oldValue or 0
newValue = plr:WaitForChild("LeaderBoardStats").NpcsServed.Value
return newValue
end)
totalMissionsCompleted:UpdateAsync(plr.userId, function(oldValue)
local newValue = oldValue or 0
newValue = plr:WaitForChild("LeaderBoardStats").MissionsCompleted.Value
return newValue
end)
totalAlienDamage:UpdateAsync(plr.userId, function(oldValue)
local newValue = oldValue or 0
newValue = plr:WaitForChild("LeaderBoardStats").TotalAlienDamage.Value
return newValue
end)
end
local success, err = pcall(updateAsync)
if success then
print("Cool beans")
else
print("No, does not work: "..err.."")
end
end)
LeaderBoardUpdater: This reads from the OrderedDataStores and displays the top 5 of each leaderboard.
This script is from ServerScriptService.
--Leaderboard2 = Total NPC's served leaderboard
--Leaderboard3 = Total missions completed leaderboard
--Leaderboard4 = Total damage dealt to aliens leaderboard
local DSService = game:GetService("DataStoreService")
local totalMoneyMade = DSService:GetOrderedDataStore("TotalMoneyMade")
local totalNpcsServed = DSService:GetOrderedDataStore("TotalNpcsServed")
local totalMissionsCompleted = DSService:GetOrderedDataStore("TotalMissionsCompleted")
local totalAlienDamage = DSService:GetOrderedDataStore("TotalAlienDamage")
local updateLeaderData = script.Parent.UpdateLeaderData
local leaderboardGUI = game.Workspace.Leaderboard.GUIBackground.SurfaceGui
local leaderboardGUI2 = game.Workspace.Leaderboard2.GUIBackground.SurfaceGui
local leaderboardGUI3 = game.Workspace.Leaderboard3.GUIBackground.SurfaceGui
local leaderboardGUI4 = game.Workspace.Leaderboard4.GUIBackground.SurfaceGui
local players = game.Players
local one = leaderboardGUI.One
local two = leaderboardGUI.Two
local three = leaderboardGUI.Three
local four = leaderboardGUI.Four
local five = leaderboardGUI.Five
function updateLeaderboard()
local moneyPage = totalMoneyMade:GetSortedAsync(false, 5)
local npcPage = totalNpcsServed:GetSortedAsync(false, 5)
local missionPage = totalMissionsCompleted:GetSortedAsync(false, 5)
local aliensPage = totalAlienDamage:GetSortedAsync(false, 5)
local topFiveAliens = aliensPage:GetCurrentPage()
local topFiveMission = missionPage:GetCurrentPage()
local topFiveNPC = npcPage:GetCurrentPage()
local topFiveMoney = moneyPage:GetCurrentPage()
for rank, data in ipairs(topFiveMoney) do
local name = players:GetNameFromUserIdAsync(data.key)
local money = data.value
-- print(name .. " Is #" .. rank .. " with $" .. data.value .. "")
if rank == 1 then
one.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
one.PlayerName.Text = name
one.DollarSign.Text = "$"
one.Stat.Text = data.value
end
if rank == 2 then
two.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
two.PlayerName.Text = name
two.DollarSign.Text = "$"
two.Stat.Text = data.value
end
if rank == 3 then
three.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
three.PlayerName.Text = name
three.DollarSign.Text = "$"
three.Stat.Text = data.value
end
if rank == 4 then
four.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
four.PlayerName.Text = name
four.DollarSign.Text = "$"
four.Stat.Text = data.value
end
if rank == 5 then
five.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
five.PlayerName.Text = name
five.DollarSign.Text = "$"
five.Stat.Text = data.value
end
end
for rank, data in ipairs(topFiveNPC) do
local name = players:GetNameFromUserIdAsync(data.key)
local money = data.value
-- print(name .. " Is #" .. rank .. " with $" .. data.value .. "")
if rank == 1 then
leaderboardGUI2.One.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI2.One.PlayerName.Text = name
leaderboardGUI2.One.Stat.Text = data.value .. " NPC's served"
end
if rank == 2 then
leaderboardGUI2.Two.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI2.Two.PlayerName.Text = name
leaderboardGUI2.Two.Stat.Text = data.value .. " NPC's served"
end
if rank == 3 then
leaderboardGUI2.Three.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI2.Three.PlayerName.Text = name
leaderboardGUI2.Three.Stat.Text = data.value .. " NPC's served"
end
if rank == 4 then
leaderboardGUI2.Four.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI2.Four.PlayerName.Text = name
leaderboardGUI2.Four.Stat.Text = data.value .. " NPC's served"
end
if rank == 5 then
leaderboardGUI2.Five.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI2.Five.PlayerName.Text = name
leaderboardGUI2.Five.Stat.Text = data.value .. " NPC's served"
end
end
for rank, data in ipairs(topFiveMission) do
local name = players:GetNameFromUserIdAsync(data.key)
local money = data.value
-- print(name .. " Is #" .. rank .. " with $" .. data.value .. "")
if rank == 1 then
leaderboardGUI3.One.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI3.One.PlayerName.Text = name
leaderboardGUI3.One.Stat.Text = data.value .. " Missions complete"
end
if rank == 2 then
leaderboardGUI3.Two.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI3.Two.PlayerName.Text = name
leaderboardGUI3.Two.Stat.Text = data.value .. " Missions complete"
end
if rank == 3 then
leaderboardGUI3.Three.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI3.Three.PlayerName.Text = name
leaderboardGUI3.Three.Stat.Text = data.value .. " Missions complete"
end
if rank == 4 then
leaderboardGUI3.Four.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI3.Four.PlayerName.Text = name
leaderboardGUI3.Four.Stat.Text = data.value .. " Missions complete"
end
if rank == 5 then
leaderboardGUI3.Five.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI3.Five.PlayerName.Text = name
leaderboardGUI3.Five.Stat.Text = data.value .. " Missions complete"
end
end
for rank, data in ipairs(topFiveAliens) do
local name = players:GetNameFromUserIdAsync(data.key)
local money = data.value
-- print(name .. " Is #" .. rank .. " with $" .. data.value .. "")
if rank == 1 then
leaderboardGUI4.One.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI4.One.PlayerName.Text = name
leaderboardGUI4.One.Stat.Text = data.value .. " Damage"
end
if rank == 2 then
leaderboardGUI4.Two.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI4.Two.PlayerName.Text = name
leaderboardGUI4.Two.Stat.Text = data.value .. " Damage"
end
if rank == 3 then
leaderboardGUI4.Three.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI4.Three.PlayerName.Text = name
leaderboardGUI4.Three.Stat.Text = data.value .. " Damage"
end
if rank == 4 then
leaderboardGUI4.Four.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI4.Four.PlayerName.Text = name
leaderboardGUI4.Four.Stat.Text = data.value .. " Damage"
end
if rank == 5 then
leaderboardGUI4.Five.PlayerIcon.Image = "https://web.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&userid="..data.key..""
leaderboardGUI4.Five.PlayerName.Text = name
leaderboardGUI4.Five.Stat.Text = data.value .. " Damage"
end
end
end
while true do
wait(30)
updateLeaderboard()
end
LeaderDataUpdateTimer: This is a LocalScript that periodically prompts the server to save the leaderdata of the client this event fired from.
This script is from StarterPlayerScripts.
if not game:IsLoaded() then
game.Loaded:Wait()
end
local updateLeaderStatData = game.ReplicatedStorage.UpdateLeaderData
while true do
wait(27)
updateLeaderStatData:FireServer()
end
I’m aware the timer gap isn’t too large, but regardless of how big I make it, it does eventually throttle.
Sorry for the hideously big post, I’m quite stuck with what to do and I need some feedback. I hope this post wasn’t too hard to read
Cheers - reddust1