[Unsolved Issue]:
My Second Leaderboard is showing the data from my First Leaderboard
So I have two leaderboards. They are supposed to show two different datastores, but both leaderboards show the same data.
Images of the leaderstats
1. My Coins leaderboard. This shows the right data for the right board.
2. My Event leaderboard, This shows the wrong data from the wrong datastore (leaderboard 1).
Scripts
Script of Leaderboard 1. (Works as intended.)
-- [ SETTINGS ] --
local StatsName = "Coins" -- Your stats name
local MaxItems = 20 -- Max number of items to be displayed on the leaderboard
local MinValueDisplay = 1 -- Any numbers lower than this will be excluded
local MaxValueDisplay = 1000000000e1000000 -- (10 ^ 15) Any numbers higher than this will be excluded
local UpdateEvery = 60 -- (in seconds) How often the leaderboard has to update
-- [ END SETTINGS ] --
-- Don't edit if you don't know what you're doing --
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("CoinsDataStore") --CoinsDataStore
local SurfaceGui = script.Parent
local Sample = script.Sample
local List = SurfaceGui.Frame.List
local ItemsFrame = List.ListContent.Items
local function GetItems()
local Data = DataStore:GetSortedAsync(false, MaxItems, MinValueDisplay, MaxValueDisplay)
local TopPage = Data:GetCurrentPage()
ItemsFrame.Nothing.Visible = #TopPage == 0 and true or false
for i, v in ipairs(TopPage) do
local UserId = v.key
local Value = v.value
local Username = "[Not Available]"
local Color = Color3.fromRGB(38, 50, 56)
local Success, Error = pcall(function()
Username = game.Players:GetNameFromUserIdAsync(UserId)
end)
if i == 1 then
Color = Color3.fromRGB(255, 215, 0)
elseif i == 2 then
Color = Color3.fromRGB(192, 192, 192)
elseif i == 3 then
Color = Color3.fromRGB(205, 127, 50)
end
local Item = Sample:Clone()
Item.Name = Username
Item.LayoutOrder = i
Item.Values.Number.TextColor3 = Color
Item.Values.Number.Text = i
Item.Values.Username.Text = Username
Item.Values.Value.Text = Value
Item.Parent = ItemsFrame
end
end
while true do
for i, v in pairs(game.Players:GetPlayers()) do
local Stats = v.leaderstats:WaitForChild(StatsName).Value
if Stats then
pcall(function()
DataStore:UpdateAsync(v.UserId, function(Value)
return tonumber(Stats)
end)
end)
end
end
for i, v in pairs(ItemsFrame:GetChildren()) do
if v:IsA("ImageLabel") then
v:Destroy()
end
end
GetItems()
wait()
List.ListContent.GuideTopBar.Value.Text = StatsName
List.CanvasSize = UDim2.new(0, 0, 0, ItemsFrame.UIListLayout.AbsoluteContentSize.Y + 35)
wait(UpdateEvery)
end
Script of Leaderboard 2. (The one that needs to be fixed)
-- [ SETTINGS ] --
local StatsName = "Events" -- Your stats name
local MaxItems = 20 -- Max number of items to be displayed on the leaderboard
local MinValueDisplay = 1 -- Any numbers lower than this will be excluded
local MaxValueDisplay = 1000000000e1000000 -- (10 ^ 15) Any numbers higher than this will be excluded
local UpdateEvery = 60 -- (in seconds) How often the leaderboard has to update
-- [ END SETTINGS ] --
-- Don't edit if you don't know what you're doing --
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("Event") --CoinsDataStore
local SurfaceGui = script.Parent
local Sample = script.Sample
local List = SurfaceGui.Frame.List
local ItemsFrame = List.ListContent.Items
local function GetItems()
local Data = DataStore:GetSortedAsync(false, MaxItems, MinValueDisplay, MaxValueDisplay)
local TopPage = Data:GetCurrentPage()
ItemsFrame.Nothing.Visible = #TopPage == 0 and true or false
for i, v in ipairs(TopPage) do
local UserId = v.key
local Value = v.value
local Username = "[Not Available]"
local Color = Color3.fromRGB(38, 50, 56)
local Success, Error = pcall(function()
Username = game.Players:GetNameFromUserIdAsync(UserId)
end)
if i == 1 then
Color = Color3.fromRGB(255, 215, 0)
elseif i == 2 then
Color = Color3.fromRGB(192, 192, 192)
elseif i == 3 then
Color = Color3.fromRGB(205, 127, 50)
end
local Item = Sample:Clone()
Item.Name = Username
Item.LayoutOrder = i
Item.Values.Number.TextColor3 = Color
Item.Values.Number.Text = i
Item.Values.Username.Text = Username
Item.Values.Value.Text = Value
Item.Parent = ItemsFrame
end
end
while true do
print("updating")
for i, v in pairs(game.Players:GetPlayers()) do
local Stats = v.leaderstats:WaitForChild(StatsName).Value
if Stats then
pcall(function()
DataStore:UpdateAsync(v.UserId, function(Value)
return tonumber(Stats)
end)
end)
end
end
for i, v in pairs(ItemsFrame:GetChildren()) do
if v:IsA("ImageLabel") then
v:Destroy()
end
end
GetItems()
wait()
List.ListContent.GuideTopBar.Value.Text = StatsName
List.CanvasSize = UDim2.new(0, 0, 0, ItemsFrame.UIListLayout.AbsoluteContentSize.Y + 35)
wait(UpdateEvery)
end
- Script 1 / Leaderboard 1 uses a currency called “Coins” and a datastore called “CoinsDataStore”
- Script 2 / Leaderboard 2 uses a currency called “Event” and a datastore called “Event”
As shown in the details, Script 2 / Leaderboard 2
shows the same data as Script 1 / Leaderboard 1
, but it should show its own data. Please help me fix this! By the way, I duplicated the coins script and modified it for the events script, so there must be something I overlooked in the second leaderboard.
Thank you!