Hey Everyone,
So I’m quite bad at datastores and making global leaderboards, so that’s why I’m using videos.
So basically, I watched a video by @xuefei123 which shows you how to make a global leaderboard with top 3 statues.
I need to configure it with my story game, which has rounds and wins . I managed to do this with the datastore script which is this:
local Players = game:GetService('Players')
local DataStoreService = game:GetService('DataStoreService')
local WinsDataStore = DataStoreService:GetDataStore('Wins')
local RoundsDataStore = DataStoreService:GetDataStore('Rounds')
Players.PlayerAdded:Connect(function(Player)
local Stats = Instance.new('Folder')
Stats.Name = 'leaderstats'
Stats.Parent = Player
local Wins = Instance.new('IntValue')
Wins.Name = 'Wins'
Wins.Parent = Stats
local Rounds = Instance.new('IntValue')
Rounds.Name = 'Rounds'
Rounds.Parent = Player
local Data = WinsDataStore:GetAsync(Player.UserId)
local Data2 = RoundsDataStore:GetAsync(Player.UserId)
if Data then
for name, value in pairs(Data.Stats) do
Stats[name].Value = value
end
end
if Data2 then
for name, value in pairs(Data2.Stats) do
Stats[name].Value = value
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
local SaveData = {Stats = {}}
local SaveData2 = {Stats = {}}
for _, stat in pairs(Player.leaderstats:GetChildren()) do
SaveData.Stats[stat.Name] = stat.Value
end
for _, stat in pairs(Player:GetChildren()) do
if stat.Name == 'Rounds' then
SaveData2.Stats[stat.Name] = stat.Value
end
end
WinsDataStore:SetAsync(Player.UserId,SaveData)
RoundsDataStore:SetAsync(Player.UserId, SaveData2)
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
local SaveData = {Stats = {}}
local SaveData2 = {Stats = {}}
for _,stat in pairs(Player.leaderstats:GetChildren()) do
SaveData.Stats[stat.Name] = stat.Value
end
for _, stat in pairs(Player:GetChildren()) do
if stat.Name == 'Rounds' then
SaveData2.Stats[stat.Name] = stat.Value
end
end
WinsDataStore:SetAsync(Player.UserId,SaveData)
RoundsDataStore:SetAsync(Player.UserId, SaveData2)
end
wait(2)
end)
(At least I think I did)
But I’m not sure how to make this work with my leaderboard script, here it is:
local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')
local GlobalDataStore = DataStoreService:GetOrderedDataStore('WinsGlobal')
local winsBoard = workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.GlobalBoard
local winsTemplate = winsBoard.SurfaceGui.Leaderboard.Template:Clone()
winsBoard.SurfaceGui.Leaderboard.Template:Destroy()
local roundsBoard = workspace.Leaderboards.RoundsLeaderboard.RoundsLeaderboard.GlobalBoard
local RoundsTemplate = roundsBoard.SurfaceGui.Leaderboard.Template:Clone()
roundsBoard.SurfaceGui.Leaderboard.Template:Destroy()
local function update()
for _,child in pairs(winsBoard.SurfaceGui.Leaderboard:GetChildren()) do
if child:IsA('Frame') then
child:Destroy()
end
end
local success,err = pcall(function()
local data = GlobalDataStore:GetSortedAsync(false,50)
local page = data:GetCurrentPage()
for rank,plrData in ipairs(page) do
local userid = plrData.key
local wins = plrData.value
if rank <= 3 then
local npc = workspace.Leaderboards.WinsLeaderboard.WinsPodium.NPCs:FindFirstChild(rank)
if npc then
npc.UserId.Value = userid
end
end
local new = winsTemplate:Clone()
new.PlrName.Text = Players:GetNameFromUserIdAsync(userid)
new.PlrAmount.Text = wins
new.LayoutOrder = rank
new.Parent = winsBoard.SurfaceGui.Leaderboard
end
end)
end
while true do
update()
wait(math.random(2,5))
spawn(function()
for _, Player in pairs(game.Players:GetPlayers()) do
GlobalDataStore:SetAsync(Player.UserId,Player.leaderstats.Wins.Value)
wait(math.random(2,4))
end
end)
end
Thanks for any help!