We have a leaderboard in our game (helping my son) but the problem is that it is showing the total time elapsed and not the time completed (e.g. If a player completes the maze in 4 minutes but stays in the game for a total of 6 minutes, this is the time that shows on the board) I am unsure of which part(s) to change in the script.
We don’t have a start part as we want the time to start as soon as you enter the game but for the leaderboard to only register the time the end part was touched (i.e. completed time) but we would still like the timer to continue to work to show the total time in game.
Hope this makes sense.
Thank you
local DataStoreService = game:GetService("DataStoreService")
local PlayerTime = DataStoreService:GetOrderedDataStore("PlayerTime")
game:GetService("Players").PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = 'leaderstats'
leaderstats.Parent = Player
local Time = Instance.new("IntValue")
Time.Name = "Time"
Time.Value = 0
Time.Parent = Player
local TimeElapsed = Instance.new("StringValue")
TimeElapsed.Name = 'Elapsed'
TimeElapsed.Value = ""
TimeElapsed.Parent = leaderstats
local TimeCompleted = Instance.new("StringValue")
TimeCompleted.Name = 'Completed'
TimeCompleted.Value = "00:00:00"
TimeCompleted.Parent = leaderstats
game:GetService("Workspace"):WaitForChild("badgePart").Touched:Connect(function(hit)
if hit.Parent ~= nil and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) ~= nil and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) == Player then
local Seconds = math.floor(Time.Value % 60)
local Minutes = math.floor(Time.Value / 60)
local Hours = math.floor(Time.Value / 3600)
local CombinedTime = string.format("%02i:%02i:%02i", Hours, Minutes, Seconds)
TimeCompleted.Value = CombinedTime
game:GetService("BadgeService"):AwardBadge(Player.UserId, 2124805029)
end
end)
spawn(function()
while wait(1) do
Time.Value += 1
local Seconds = math.floor(Time.Value % 60)
local Minutes = math.floor(Time.Value / 60)
local Hours = math.floor(Time.Value / 3600)
local CombinedTime = string.format("%02i:%02i:%02i", Hours, Minutes, Seconds)
TimeElapsed.Value = CombinedTime
end
end)
end)
local function updateLeaderboard()
local success, errorMessage = pcall (function()
local Data = PlayerTime:GetSortedAsync(false,10)
local TimerPage = Data:GetCurrentPage()
for Rank, Data in ipairs(TimerPage) do
local Username = game:GetService("Players"):GetNameFromUserIdAsync(tonumber(Data.key))
local Time = Data.value
local IsOnLeaderboard = false
for i, v in pairs(game:GetService("Workspace"):WaitForChild("leaderboard"):WaitForChild("leaderboardGui"):WaitForChild("Backboard"):GetChildren()) do
if v:WaitForChild("Player").Text == Username then
IsOnLeaderboard = true
break
end
end
if Time and IsOnLeaderboard == false then
local newLBFrame = game:GetService("ReplicatedStorage"):WaitForChild("dataholder"):Clone()
newLBFrame:WaitForChild("Player").Text = Username
newLBFrame:WaitForChild("Time").Text = Time
newLBFrame:WaitForChild("Rank").Text = "#"..Rank
newLBFrame.Position = UDim2.new(0, 0, newLBFrame.Position.Y.Scale + (.08 * #game:GetService("Workspace"):WaitForChild("leaderboard"):WaitForChild("leaderboardGui"):WaitForChild("Backboard"):GetChildren()),0)
newLBFrame.Parent = game:GetService("Workspace"):WaitForChild("leaderboard"):WaitForChild("leaderboardGui"):WaitForChild("Backboard")
end
end
end)
if success then
warn ("Successfully updated the leaderboard.")
else
warn ("Unsuccessfully updated the leaderboard. Error: \n"..errorMessage)
end
end
while wait(10) do
for _, player in pairs(game.Players:GetPlayers()) do
local Success, Error = pcall(function()
PlayerTime:SetAsync(player.UserId, player:WaitForChild("Time").Value)
end)
if Success then
print ("Successfully updated the players datastore in the while loop.")
else
warn ("Unsuccessfully updated the players datastore in the while loop. Error: \n"..Error)
end
end
for _, frame in pairs(game.Workspace.leaderboard.leaderboardGui.Backboard:GetChildren()) do
frame:Destroy()
end
updateLeaderboard()
end