Can any of you on here give me guidance on something? I’ve tried several tutorials none of them worked no matter what. All I need to know is how I can put a timing on a leaderboard if that makes any sense at all. I’m trying to add a time trial system to my obby so people can have more fun but i’m on the verge of throwing my computer in the garbage!
If you feel like helping just type under this thread, any and all help is needed and my scripts are below!
Leaderboard script:
local dataStoreService = game:GetService("DataStoreService")
local leaderboardStore = dataStoreService:GetOrderedDataStore("Leaderboard")
local Players = game:GetService("Players")
replicatedStorage.BestTime.OnServerEvent:Connect(function(player, time)
time = math.floor(time * 1000)
leaderboardStore:UpdateAsync(player.UserId, function(oldTime)
if not oldTime then
oldTime = math.huge
end
if time < oldTime then
return time
else
return
end
end)
end)
local LeaderPart = workspace:WaitForChild("LeaderPart")
local surfacegui = LeaderPart.LeaderboardGui
local positionLabels = {
surfacegui:WaitForChild(1),
surfacegui:WaitForChild(2),
surfacegui:WaitForChild(3),
surfacegui:WaitForChild(4),
surfacegui:WaitForChild(5),
}
local function updateLeaderboard()
local leaderboardData = leaderboardStore:GetSortedAsync(true, 5)
local currentpage = leaderboardData:GetCurrentPage()
for i, data in ipairs(currentpage) do
local time = data.value/1000
local positionLabel = positionLabels[i]
if positionLabel then
local player = game:GetService("Players"):GetNameFromUserIdAsync(data.key)
if player then
positionLabel.Text = string.format("%d. %s - %.3f", i, player, time)
else
positionLabel.Text = ""
end
end
end
end
replicatedStorage.BestTime.OnServerEvent:Connect(function(player, time)
time = math.floor(time * 1000)
leaderboardStore:SetAsync(player.UserId, time)
updateLeaderboard()
end)
updateLeaderboard()
TImerDataSaving Script:
```lua local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("PersonalBest")
local replicatedStorage = game:GetService("TimeFolder")
game.Players.PlayerAdded:Connect(function(plr)
local BestTime = Instance.new("NumberValue", plr)
BestTime.Name = "PersonalBest"
BestTime.Value = dataStore:GetAsync("Best_" .. plr.UserId) or math.huge
end)
replicatedStorage:WaitForChild("TimeFolder").OnServerEvent:Connect(function(plr, personalBest)
plr.PersonalBest.Value = personalBest
end)
game.Players.PlayerRemoving:Connect(function(plr)
dataStore:SetAsync("Best_" .. plr.UserId, plr.PersonalBest.Value)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
dataStore:SetAsync("Best_" .. plr.UserId, plr.PersonalBest.Value)
end
end)
Local Timer Script:
```lua local startTime = 0
local currentTime = 0
local timerStopped = false
local Runservice = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local personalBest = player:WaitForChild("PersonalBest") or math.huge
local replicatedStorage = game:GetService("TimeFolder")
local textLabel = script.Parent
textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
personalBest.Changed:Connect(function()
textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
end)
function onStartTouched(hit)
timerStopped = false
startTime = tick()
currentTime = 0
Runservice.RenderStepped:Connect(function()
if not timerStopped then
currentTime = tick() - startTime
textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
end
end)
end
function onEndTouched(hit)
if not timerStopped then
timerStopped = true
if currentTime > 0 and currentTime < personalBest.Value then
personalBest.Value = currentTime
replicatedStorage.BestTime:FireServer(currentTime)
end
textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
end
end
workspace:WaitForChild("StartTimer").Touched:Connect(onStartTouched)
workspace:WaitForChild("EndTimer").Touched:Connect(onEndTouched)