I am making 3 podium places for my leader board in game, however, I have been struggling in trying to get the UserId for the top 3 placements on the leader board, would anyone know how to do this?
Script
local AbbreviateNumber = require(game.ReplicatedStorage:WaitForChild("AbbreviateNumber"))
local sg = script.Parent --Surface GUI
local sample = script:WaitForChild("Sample") --Sample frame
local sf = sg:WaitForChild("ScrollingFrame") --The scrolling frame
local ui = sf:WaitForChild("UI") --UI list layout
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetOrderedDataStore("CoinLeaderboard")
wait(5)
while true do
--loop that runs every 2 minutes
for i,plr in pairs(game.Players:GetChildren()) do--Loop through players
if plr.UserId>0 then--Prevent errors
local w = plr.leaderstats.Coins.Value
if w then
pcall(function()
dataStore:UpdateAsync(plr.UserId,function(oldVal)
--Set new value
return tonumber(w)
end)
end)
end
end
end
local smallestFirst = false--false = 2 before 1, true = 1 before 2
local numberToShow = 100--how many players will be shown
local minValue = 1--Any numbers lower than this will be excluded
local maxValue = 10e30--(10^30), any numbers higher than this will be excluded
local pages = dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
--Get data
local top = pages:GetCurrentPage()--Get the first page
local data = {}--Store new data
for a,b in ipairs(top) do--Loop through data
local userid = b.key--User id
local points = b.value--Points
local username = "[Failed To Load]"
local s,e = pcall(function()
username = game.Players:GetNameFromUserIdAsync(userid)--Get username
end)
if not s then
warn("Error getting name for "..userid..". Error: "..e)
end
table.insert(data,{username,points})--Put new data in new table
end
ui.Parent = script
sf:ClearAllChildren()--Remove old frames
ui.Parent = sf
for number,d in pairs(data) do--Loop through our new data
local name = d[1]
local val = d[2]
local color = Color3.new(1,1,1)--Default color
if number == 1 then
color = Color3.new(1,1,0)--1st place color
elseif number == 2 then
color = Color3.new(0.9,0.9,0.9)--2nd place color
elseif number == 3 then
color = Color3.fromRGB(166, 112, 0)--3rd place color
end
local new = sample:Clone()--Make a clone of the sample frame
new.Name = name--Set name of frame
new.LayoutOrder = number
new.ImageLabel.playerName.Text = name--Set the username
new.ImageLabel.rank.Text = "#"..number--Set the place
new.ImageLabel.rank.TextColor3 = color--Set the place color
new.ImageLabel.value.Text = AbbreviateNumber:Abbreviate(val)--Set the amount of points
new.ImageLabel.value.TextColor3 = color--Set the place color
new.ImageLabel.playerName.TextColor3 = color--Set the place color
new.Parent = sf--Parent to scrolling frame
end
wait()
sf.CanvasSize = UDim2.new(0,0,0,ui.AbsoluteContentSize.Y)
--Give enough room for the frames to sit in
wait(180)
end
Once I have the UserId I would be fine loading in the characters.