The issue is I have 3 leaderboards, and I need them to update. However, when Roblox Datastore has 2 leaderboards update at the same time, the whole thing breaks and starts assigning values to the wrong leaderboard. I tried to use Chinese Remainder Theorem where we have the timers start at different times, and we use prime numbers for how long it refreshes. 5, 7, 11. This SHOULD work but roblox servers are so slow they line up eventually, causing the same problem. Is there a way to avoid this problem? Here is the code to one of the leaderboards, all 3 are the same just with different values.
local coinsODS = ds:GetOrderedDataStore("PlayerData")
local timeUntilReset = 7
task.wait(1)
while task.wait(1) do
timeUntilReset = timeUntilReset - 1
script.Parent.Parent.ResetTime.Text = "Resetting in " .. timeUntilReset .. " seconds..."
if timeUntilReset == 0 then
timeUntilReset = 10
for i, plr in pairs(game.Players:GetPlayers()) do
coinsODS:SetAsync(plr.UserId, plr.DataFolder.Kills.Value)
end
for i, leaderboardRank in pairs(script.Parent:GetChildren()) do
if leaderboardRank.ClassName == "Frame" then
leaderboardRank:Destroy()
end
end
local success, errorMsg = pcall(function()
local data = coinsODS:GetSortedAsync(false, 50)
local coinsPage = data:GetCurrentPage()
for rankInLB, dataStored in ipairs(coinsPage) do
local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
local coins = dataStored.value
local template = script.Template:Clone()
template.Name = name .. "Leaderboard"
template.PlrName.Text = name
template.Rank.Text = "#" .. rankInLB
template.Coins.Text = coins
template.Parent = script.Parent
end
end)
end
end```
im not an expert on roblox datastore but if assigning two leaderboards at the same time causes a datastore crossover, then what sort of horrible system is roblox running
there seems to be nothing wrong with your script at all, maybe one of your other three has an issue
there are some things i did notice though (not related to datastores)
is this intentional?
good idea to pcall this
jst incase you didnt know pairs() is obsolete, you can just do
for i, leaderboardRank in script.Parent:GetChildren() do
That is absolutely not normal, and I don’t really see how it could be happening unless you are using the same datastore and the same key for your 2 or 3 leaderboards, somehow?
Could you show the code for your other leaderboards?
local coinsODS = ds:GetOrderedDataStore("PlayerData")
local timeUntilReset = 5
while task.wait(1) do
timeUntilReset = timeUntilReset - 1
script.Parent.Parent.ResetTime.Text = "Resetting in " .. timeUntilReset .. " seconds..."
if timeUntilReset == 0 then
timeUntilReset = 5
for i, plr in pairs(game.Players:GetPlayers()) do
coinsODS:SetAsync(plr.UserId, plr.DataFolder.Beans.Value)
end
for i, leaderboardRank in pairs(script.Parent:GetChildren()) do
if leaderboardRank.ClassName == "Frame" then
leaderboardRank:Destroy()
end
end
local success, errorMsg = pcall(function()
local data = coinsODS:GetSortedAsync(false, 50)
local coinsPage = data:GetCurrentPage()
for rankInLB, dataStored in ipairs(coinsPage) do
local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
local coins = dataStored.value
local template = script.Template:Clone()
template.Name = name .. "Leaderboard"
template.PlrName.Text = name
template.Rank.Text = "#" .. rankInLB
template.Coins.Text = coins
template.Parent = script.Parent
end
end)
end
end```
**second one**
```local ds = game:GetService("DataStoreService")
local coinsODS = ds:GetOrderedDataStore("PlayerData")
local timeUntilReset = 7
task.wait(1)
while task.wait(1) do
timeUntilReset = timeUntilReset - 1
script.Parent.Parent.ResetTime.Text = "Resetting in " .. timeUntilReset .. " seconds..."
if timeUntilReset == 0 then
timeUntilReset = 7
for i, plr in pairs(game.Players:GetPlayers()) do
coinsODS:SetAsync(plr.UserId, plr.DataFolder.Kills.Value)
end
for i, leaderboardRank in pairs(script.Parent:GetChildren()) do
if leaderboardRank.ClassName == "Frame" then
leaderboardRank:Destroy()
end
end
local success, errorMsg = pcall(function()
local data = coinsODS:GetSortedAsync(false, 50)
local coinsPage = data:GetCurrentPage()
for rankInLB, dataStored in ipairs(coinsPage) do
local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
local coins = dataStored.value
local template = script.Template:Clone()
template.Name = name .. "Leaderboard"
template.PlrName.Text = name
template.Rank.Text = "#" .. rankInLB
template.Coins.Text = coins
template.Parent = script.Parent
end
end)
end
end```
**third one**
```local ds = game:GetService("DataStoreService")
local coinsODS = ds:GetOrderedDataStore("PlayerData")
local timeUntilReset = 11
task.wait(3)
while task.wait(1) do
timeUntilReset = timeUntilReset - 1
script.Parent.Parent.ResetTime.Text = "Resetting in " .. timeUntilReset .. " seconds..."
if timeUntilReset == 0 then
timeUntilReset = 11
for i, plr in pairs(game.Players:GetPlayers()) do
coinsODS:SetAsync(plr.UserId, plr.DataFolder.Nights.Value)
end
for i, leaderboardRank in pairs(script.Parent:GetChildren()) do
if leaderboardRank.ClassName == "Frame" then
leaderboardRank:Destroy()
end
end
local success, errorMsg = pcall(function()
local data = coinsODS:GetSortedAsync(false, 50)
local coinsPage = data:GetCurrentPage()
for rankInLB, dataStored in ipairs(coinsPage) do
local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
local coins = dataStored.value
local template = script.Template:Clone()
template.Name = name .. "Leaderboard"
template.PlrName.Text = name
template.Rank.Text = "#" .. rankInLB
template.Coins.Text = coins
template.Parent = script.Parent
end
end)
end
end```
If you use a different datastore than "PlayerData" for your other two leaderboards, you will avoid this issue entirely.
For each player, you are constantly overwriting his leaderboard value with another leaderboard value. Instead, by using 3 different datastores, the player will have his 3 leaderboard values stored separately, and without interfering with each other
This will also fix another issue, where, for offline players, only 1 of the 3 leaderboard values is working, and that value is being used for all 3 leaderboards, for the same reason as above
All you have to do is change this line,
local coinsODS = ds:GetOrderedDataStore("PUT_NEW_NAME_HERE")
and give each leaderboard its own unique name
Also, no need for the Chinese Remainder Theorem with this change