I Have A Global Leaderboard And Leaderstats They Work Just Fine But When I Added A DataStore To Save The Data It Shows The Leaderstat as 0 And Doesn't Show Anything Here Are My Scripts:
This Is The Leaderstats And DataStore Script
local dataStoreService = game:GetService('DataStoreService')
local myDataStore = dataStoreService:GetOrderedDataStore('WinsLeaderBoard')
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local Wins = Instance.new('IntValue')
Wins.Name = 'Wins'
Wins.Parent = leaderstats
Wins.Value = 11
local Coins = Instance.new('IntValue')
Coins.Name = 'Coins'
Coins.Parent = leaderstats
Coins.Value = 0
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId..'-Wins')
end)
if data ~= nil then
Wins.Value = data
else
print('There Was An Error Saving Data!')
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId..'-Wins',player.leaderstats.Wins.Value)
end)
if success then
print('Saved Data Successfuly Saved!')
else
print('There Was An Error While Saving Data!')
warn(errormessage)
end
end)
This Is The Script For The LeaderBoard
local DataStoreService = game:GetService('DataStoreService')
local WinsDataStore = DataStoreService:GetOrderedDataStore('WinsLeaderBoard')
local LeaderBoardPart = script.Parent.Parent.Parent
local RefreshRate = 10
local function RefreshLeaderboard()
for i,Player in pairs(game.Players:GetPlayers()) do
WinsDataStore:SetAsync(Player.UserId, Player.leaderstats.Wins.Value)
end
local Success, Error = pcall(function()
local Data = WinsDataStore:GetSortedAsync(false, 10)
local WinsPage = Data:GetCurrentPage()
for Rank, SavedData in ipairs(WinsPage) do
local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key))
local Wins = SavedData.value
if Wins then
local NewSample = script.Parent.GLTemplate:Clone()
NewSample.Visible = true
NewSample.Parent = LeaderBoardPart.SurfaceGui.Holder
NewSample.Name = Username
NewSample.Rank.Text = '#'..Rank
NewSample.PlayerName.Text = Username
NewSample.Value.Text = Wins
end
end
end)
end
while true do
for i,Frame in pairs(LeaderBoardPart.SurfaceGui.Holder:GetChildren()) do
if Frame.Name ~= 'GLTemplate' and Frame:IsA('Frame') then
Frame:Destroy()
end
end
RefreshLeaderboard()
task.wait(RefreshRate)
end
Thanks.
