- What do you want to achieve? i am trying to make chat tags for the top 3 players on the global leaderboard but it wont get the players rank.
help would be appericated!
-custom leaderboard script
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local TempPart = script.Parent
local SurfaceGui = TempPart.Parent.AllTime
local MainFrame = SurfaceGui.MainFrame
local Template = script:WaitForChild("Template")
local RecentlySaved = {}
local GlobalLeaderboardB = DataStoreService:GetOrderedDataStore("Game:GlobalCoins")
local LeaderboardDataStore = DataStoreService:GetDataStore("LeaderboardRanks")
local CurrencyName = "Points"
local ListSize = 100
local UpdateEvery = 50
local MinimumRequirement = 1
local _functions = {}
_functions.returncurrency = function(player)
local currencyValue = 0
for _, descendant in pairs(player:GetDescendants()) do
if (descendant:IsA("IntValue") or descendant:IsA("NumberValue")) and (descendant.Name == CurrencyName) then
currencyValue = descendant.Value
break
end
end
return currencyValue
end
_functions.removefromrecentlysaved = function(playerName)
for i, savedName in pairs(RecentlySaved) do
if savedName == playerName then
table.remove(RecentlySaved, i)
break
end
end
end
_functions.autoremoverecentsaved = function(playerName)
task.spawn(function()
wait(15)
_functions.removefromrecentlysaved(playerName)
end)
end
_functions.returnplayerlist = function()
local count = 0
for _, player in pairs(Players:GetPlayers()) do
for _, descendant in pairs(player:GetDescendants()) do
if descendant.Name == CurrencyName and descendant.Value > MinimumRequirement then
count = count + 1
break
end
end
end
return count
end
_functions.clear = function()
for _, child in pairs(MainFrame:GetChildren()) do
if child:IsA('Frame') then
child:Destroy()
end
end
end
_functions.abbreviate = function(value, idp)
if value < 1000 then
return math.floor(value + 0.5)
else
local abbreviations = {"", "K", "M", "B", "T"}
local exponent = math.floor(math.log(math.max(1, math.abs(value)), 1000))
local abbreviation = abbreviations[1 + exponent] or ("e+" .. exponent)
local normal = math.floor(value * ((10 ^ idp) / (1000 ^ exponent))) / (10 ^ idp)
return ("%." .. idp .. "f%s"):format(normal, abbreviation)
end
end
local function saveLeaderboardRanks(ranks)
local success, errorMessage = pcall(function()
LeaderboardDataStore:SetAsync("LeaderboardRanks", ranks)
end)
if not success then
warn("Failed to save leaderboard ranks: " .. tostring(errorMessage))
else
print("Successfully saved leaderboard ranks")
end
end
local function loadLeaderboardRanks()
local success, ranks = pcall(function()
return LeaderboardDataStore:GetAsync("LeaderboardRanks")
end)
if success then
return ranks or {}
else
warn("Failed to load leaderboard ranks: " .. tostring(ranks))
return {}
end
end
-- Load the leaderboard ranks when the server starts
local leaderboardRanks = loadLeaderboardRanks()
Players.PlayerRemoving:Connect(function(player)
local playerName = player.Name
if not table.find(RecentlySaved, playerName) then
table.insert(RecentlySaved, playerName)
local currentCurrencyAmount = _functions.returncurrency(player)
local success, errorMessage = pcall(function()
GlobalLeaderboardB:SetAsync(player.UserId, currentCurrencyAmount)
end)
if not success then
warn("Failed to save player data: " .. tostring(errorMessage))
else
print("Successfully saved data for player: " .. playerName)
end
_functions.removefromrecentlysaved(playerName)
end
end)
while true do
_functions.clear()
repeat wait() until _functions.returnplayerlist() > 0
for _, player in pairs(Players:GetPlayers()) do
local playerName = player.Name
if not table.find(RecentlySaved, playerName) then
table.insert(RecentlySaved, playerName)
local currentCurrencyAmount = _functions.returncurrency(player)
local success, errorMessage = pcall(function()
GlobalLeaderboardB:SetAsync(player.UserId, currentCurrencyAmount)
end)
if not success then
warn("Failed to save player data: " .. tostring(errorMessage))
else
print("Successfully saved data for player: " .. playerName)
end
_functions.autoremoverecentsaved(playerName)
end
end
local success, pages = pcall(function()
return GlobalLeaderboardB:GetSortedAsync(false, ListSize)
end)
if not success then
warn("Failed to get sorted async data: " .. tostring(pages))
task.wait(UpdateEvery)
continue
end
local topList = pages:GetCurrentPage()
local newLeaderboardRanks = {}
for i, data in ipairs(topList) do
local userId = tonumber(data.key)
if userId then
local rank = i
newLeaderboardRanks[userId] = rank
print("Assigning Rank:", rank, "to UserId:", userId)
local user = "Unknown"
local success, errorMessage = pcall(function()
user = Players:GetNameFromUserIdAsync(userId)
end)
if not success then
warn("Failed to get player name: " .. tostring(errorMessage))
end
if rank <= 10 then
local templateClone = Template:Clone()
templateClone.Parent = MainFrame
templateClone.LayoutOrder = rank
templateClone.Score.Text = _functions.abbreviate(data.value, 1)
templateClone.Username.Text = user
templateClone.Name = user
local function setRankImage(rank)
local imageIds = {
[1] = "rbxassetid://18775912560",
[2] = "rbxassetid://18775914469",
[3] = "rbxassetid://18775916016"
}
local rankImage = templateClone.Rank:FindFirstChild("Image")
if rankImage and imageIds[rank] then
rankImage.Image = imageIds[rank]
rankImage.ImageTransparency = 0
templateClone.Rank.TextTransparency = 1
else
templateClone.Rank.Text = "#" .. rank
if rankImage then
rankImage.ImageTransparency = 1
end
templateClone.Rank.TextTransparency = 0
end
end
setRankImage(rank)
end
else
warn("Failed to convert data.key to UserId:", data.key)
end
end
print("newLeaderboardRanks:", newLeaderboardRanks)
saveLeaderboardRanks(newLeaderboardRanks)
leaderboardRanks = newLeaderboardRanks
local updateEvent = ReplicatedStorage:FindFirstChild("UpdateLeaderboardRanks")
if updateEvent then
updateEvent:FireAllClients(newLeaderboardRanks)
else
warn("UpdateLeaderboardRanks event not found")
end
task.wait(UpdateEvery)
end
-local script
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local leaderboardRanks = {}
local updateLeaderboardRanksEvent = ReplicatedStorage:WaitForChild("UpdateLeaderboardRanks")
updateLeaderboardRanksEvent.OnClientEvent:Connect(function(topRanks)
leaderboardRanks = topRanks
end)
TextChatService.OnIncomingMessage = function(message)
local props = Instance.new("TextChatMessageProperties")
local userId = message.TextSource and message.TextSource.UserId
if userId and leaderboardRanks[userId] then
local rank = leaderboardRanks[userId]
if rank == 1 then
props.PrefixText = "<font color='#FFD700'>[1st]</font> " .. message.PrefixText
elseif rank == 2 then
props.PrefixText = "<font color='#C0C0C0'>[2nd]</font> " .. message.PrefixText
elseif rank == 3 then
props.PrefixText = "<font color='#CD7F32'>[3rd]</font> " .. message.PrefixText
else
props.PrefixText = message.PrefixText
end
else
print("Rank not found for UserId: ", userId)
props.PrefixText = message.PrefixText
end
props.Text = message.Text
return props
end