I Am Making A Game And I Have A Global Leaderboard In Spawn That Shows Some Player Stats It Works Fine But I Also Have A Leaderstats With A DataStore But The Leaderstat Does Not Work With The LeaderBoard I Have No Idea Here Are The Script’s:
This Is The Script For My Leaderstats
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 = 169
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 success then
Wins.Value = data
else
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('Player Data Saved!')
else
print('There Was An Error Saving Data!')
wait(errormessage)
end
end)
And Here 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
Sorry If This Is Confusing.