local Ds = game:GetService("DataStoreService")
local PointsLeaderboard = Ds:GetOrderedDataStore("PointsLeaderboard")
local function updateLeaderboard()
local success, errorMessage = pcall(function()
local Data = PointsLeaderboard:GetSortedAsync(false, 5)
local PointsPage = Data:GetCurrentPage()
for Rank, data in ipairs(PointsPage) do
local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = userName
local Points = data.value
local isonLeaderboard = false
for i, v in pairs(game.Workspace.GlobalLeaderboard.Board.LeaderboardGui.ScrollingFrame.Holder:GetChildren()) do
if v.Player.Text == Name then
isonLeaderboard = true
break
end
end
if Points and isonLeaderboard == false then
local newLBFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
newLBFrame.Player.Text = Name
newLBFrame.Point.Text = Points
newLBFrame.Rank.Text = "#"..Rank
newLBFrame.Position = UDim2.new(0, 0, newLBFrame.Position.Y.Scale + (.08 * #game.Workspace.GlobalLeaderboard.Board.LeaderboardGui.ScrollingFrame.Holder:GetChildren()), 0)
newLBFrame.Parent = game.Workspace.GlobalLeaderboard.Board.LeaderboardGui.ScrollingFrame.Holder
end
end
end)
if not success then
print(errorMessage)
end
end
while true do
for _, player in pairs(game.Players:GetPlayers()) do
PointsLeaderboard:SetAsync(player.UserId, player.leaderstats.Points.Value)
end
for _, frame in pairs(game.Workspace.GlobalLeaderboard.Board.LeaderboardGui.ScrollingFrame.Holder:GetChildren()) do
frame:Destroy()
end
updateLeaderboard()
print("Updated")
wait(120)
end
This script handler updates the leaderboard accordingly to the player’s stats every 120 second/2 minutes
If players have much numbers how do I abbreviate it?
function foo(n)
if n >= 10^6 then
return string.format("%.2fm", n / 10^6)
elseif n >= 10^3 then
return string.format("%.2fk", n / 10^3)
else
return tostring(n)
end
end
It’s been awhile since I’ve used it so it may or may not work still. Although it should.
function FancyNumber(numb)
if not (numb >= 1000000) then
if numb >= 10000 then
numb = string.format("%.0f", tostring(numb / 1000)) .. "K"
end
return numb
end
numb = string.format("%.2f", tostring(numb / 1000000))
for h = #numb, 1, -1 do
if numb:sub(h, h) ~= "0" and numb:sub(h, h) ~= "." then
break
end
numb = numb:sub(1, h- 1)
end
numb = numb .. "M"
return numb
end
You could just take the if statement and dumb it right in the middle of your code. But since functions are helpful. You should just copy the function and paste it anywhere in your script. Then you can call it.
Example:
foo(variable) // Calls Function : Remember to put a parameter inside if you need to.
function foo(variable)
// Code goes here
end
You should look up some more information about functions. Here is some that I already found. Programming in Lua : 5 // Lua Wiki on Functions Programming - Functions // Random University Website that seems very good. (Do not take everything from here, it is likely written in another language. But the info is good.)
Edit: It is late for me so I shall now sleep, I hope what I posted can help. I’ll probably not reply until late tomorrow.
function AbbreviateNumber(Number, Decimals)
local Suffixes = {
"K",
"M",
"B",
"T"
}
for Count = #Suffixes, 1, -1 do
if Number >= 10^(Count * 3) then
Number = string.format("%."..Decimals.."f"..Suffixes[Count], Number / 10^(Count * 3))
Number = string.gsub(Number, "%.*0+"..Suffixes[Count], Suffixes[Count])
return Number
end
end
return tostring(Number)
end
print(AbbreviateNumber(1000, 2))
print(AbbreviateNumber(1250, 2))
print(AbbreviateNumber(1250, 1))
to add more suffixes past a trillion, just add more suffixes in the suffixes table
I basically took @MrMoled’s code but used a for loop instead, this way adding more suffixes is easier
I also changed the code to remove unnecessary zeros
Before: 1.00K
After: 1K
Code: AbbreviateNumber(1000, 3)
Oooooooooo This method is much more efficient when adding more suffixes than @MrMoled !!!
but I cant give you the solution mark because I was able to understood how abbreviation works because of @MrMoled 's explanation…