Hey Everyone,
I’m making a global leaderboard for my story game, and I’m trying to make it so that on the podiums it displays the player’s names and wins amount. I have some NPCs which change to the player character.
Here is my script:
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 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
if npc.Name == '1' then
print('npc name == 1')
local PlrName11Name = game.Players:GetPlayerByUserId(userid)
game.Workspace.Leaderboards.WinsLeaderboard.WinsPodium.Podium.MiddleBlock.SurfaceGui.WinsAmount.Text = plrData.value..' Wins'
game.Workspace.Leaderboards.WinsLeaderboard.WinsPodium.Podium.MiddleBlock.SurfaceGui.PlayerName.Text = PlrName11Name.Name
elseif npc.Name == '2' then
local PlrName11Name = game.Players:GetPlayerByUserId(userid)
game.Workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.WinsPodium.Podium.RightBlock.SurfaceGui.WinsAmount.Text = plrData.value..' Wins'
game.Workspace.Leaderboards.WinsLeaderboard.WinsPodium.Podium.RightBlock.SurfaceGui.PlayerName.Text = PlrName11Name.Name
elseif npc.Name == '3' then
local PlrName11Name = game.Players:GetPlayerByUserId(userid)
game.Workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.WinsPodium.Podium.LeftBlock.SurfaceGui.WinsAmount.Text = plrData.value..' Wins'
game.Workspace.Leaderboards.WinsLeaderboard.WinsPodium.Podium.LeftBlock.SurfaceGui.PlayerName.Text = PlrName11Name.Name
end
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(120,180))
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
The problem is, that only the first NPC Changes to the player name, the other don’t. Thanks for any help!
2 Likes
I need each stand to show each players name, but currently only the 1 stand shows the name and amount of wins.
Try to replace:
if npc.Name == '1' then
...
elseif npc.Name == '2' then
...
elseif npc.Name == '3' then
to
if rank == 1 then
...
elseif rank == 2 then
...
elseif rank == 3 then
This might work.
1 Like
I changed it to this:
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 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
if rank == 1 then
print('npc name == 1')
local PlrName11Name = game.Players:GetPlayerByUserId(userid)
game.Workspace.Leaderboards.WinsLeaderboard.WinsPodium.Podium.MiddleBlock.SurfaceGui.WinsAmount.Text = plrData.value..' Wins'
game.Workspace.Leaderboards.WinsLeaderboard.WinsPodium.Podium.MiddleBlock.SurfaceGui.PlayerName.Text = PlrName11Name.Name
elseif rank == 2 then
print('Rank == 2!')
local PlrName11Name = game.Players:GetPlayerByUserId(userid)
game.Workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.WinsPodium.Podium.RightBlock.SurfaceGui.WinsAmount.Text = plrData.value..' Wins'
game.Workspace.Leaderboards.WinsLeaderboard.WinsPodium.Podium.RightBlock.SurfaceGui.PlayerName.Text = PlrName11Name.Name
elseif rank == 3 then
print('RAnk == 3!')
local PlrName11Name = game.Players:GetPlayerByUserId(userid)
game.Workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.WinsPodium.Podium.LeftBlock.SurfaceGui.WinsAmount.Text = plrData.value..' Wins'
game.Workspace.Leaderboards.WinsLeaderboard.WinsPodium.Podium.LeftBlock.SurfaceGui.PlayerName.Text = PlrName11Name.Name
end
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(120,180))
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
Rank = 1 and rank = 2 and rank = 3 prints but it only changes the first one.
1 Like
Nevermind it was my bad sorry.
There is another problem. They don’t update. How can i make it update every time the update function is ran? I reset my wins to 0 however it still says 3!
Working with DataStores can be quite a challenge. Especially when setting up a global value that can be fetched by every sever in the game.
For example, you shouldn’t be using DataStore:SetAsync
when updating a value. Instead, you should be using DataStore:UpdateAsync
.
You should read DataStores to get more familiar on how saving data works. There’s also a ModuleScript called DataStore2 you can check-out, it makes dealing DataStores much easier.