I have a football game that has a physical time played leaderboard, and what I want to do is create a visual leaderboard that is synchronized to the physical leadearboard, but I have no Idea how to do this…
I tried a lot of times but it is still not running
Here is the script I made for the visual leaderboard, it is in the serverscriptservice:
local Players = game:GetService("Players")
-- When a player joins the game
Players.PlayerAdded:Connect(function(player)
-- Check if the leaderstats folder exists, if not, create one
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
-- Ensure "Cash" continues to exist
local cashStat = leaderstats:FindFirstChild("Cash")
if not cashStat then
cashStat = Instance.new("IntValue")
cashStat.Name = "Cash"
cashStat.Value = 0 -- Or the correct initial value for Cash
cashStat.Parent = leaderstats
end
-- Create the "Time" leaderstat without affecting others
local timeStat = leaderstats:FindFirstChild("Time")
if not timeStat then
timeStat = Instance.new("IntValue")
timeStat.Name = "Time"
timeStat.Parent = leaderstats
end
-- Update "Time" based on the Time Played Leaderboard
local function updateTime()
local leaderboard = game.Workspace:FindFirstChild("TimePlayedLeaderboard")
if leaderboard then
local playerEntry = leaderboard:FindFirstChild(player.Name)
if playerEntry then
timeStat.Value = playerEntry.Value -- Synchronize with the physical leaderboard
end
end
end
-- Loop to update in real-time without freezing the game
task.spawn(function()
while player.Parent do
updateTime()
task.wait(1) -- Update every second
end
end)
end)
The physical leaderboard is using a TextLabel with a formatted time-played value. If you want to synchronize it with the Roblox leaderboard, you will need to first convert & unformat the time-played text into an integer (or float) of seconds, minutes, hours, or any other standard format before applying it.
E.g. using the example that you have provided, you will need to convert your time-played physical leaderboard value of “00d: 05h: 44m” into 5.7333… (as an example, I have converted it into an hours format) which you can then apply to your Roblox leaderboard.
local ServerStorage = game:GetService("ServerStorage")
local function parseTimeString(timeString)
local days, hours, minutes = string.match(timeString, "(%d+)d%s*:%s*(%d+)h%s*:%s*(%d+)m")
days = tonumber(days) or 0
hours = tonumber(hours) or 0
minutes = tonumber(minutes) or 0
return (days * 1440) + (hours * 60) + minutes -- Convertendo para minutos totais
end
local function updateLeaderstats()
for _, player in pairs(PlayersService:GetPlayers()) do
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local timeStat = leaderstats:FindFirstChild("Time")
if timeStat then
local leaderboard = ServerStorage:FindFirstChild("TopTimePlayedLeaderboard")
if leaderboard then
local scoreBlock = leaderboard:FindFirstChild("ScoreBlock")
if scoreBlock then
local leaderboardGui = scoreBlock:FindFirstChild("Leaderboard")
if leaderboardGui then
for _, label in pairs(leaderboardGui.Score:GetChildren()) do
if label:IsA("TextLabel") and label.Visible then
local playerNameLabel = leaderboardGui.Names:FindFirstChild("Name" .. string.sub(label.Name, 6))
if playerNameLabel and playerNameLabel.Text == player.Name then
local convertedTime = parseTimeString(label.Text)
timeStat.Value = convertedTime
end
end
end
end
end
end
end
end
end
end
PlayersService.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local timeStat = Instance.new("IntValue")
timeStat.Name = "Time"
timeStat.Parent = leaderstats
end)
while true do
updateLeaderstats()
task.wait(1) -- Atualiza a cada segundo
end