I need help with my leaderboard leaderstats

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! :skull: 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)

Die you find any errors inside the output? If not, try to rename the time variable as it’s interfering with some Roblox library. There are 2 BestTime events too, is there any reason for that? The second event just sets the time*1000 as the time and updates the leaderboard.

1 Like

I’ll rename it to “TimedEvent” or something but which timed event section of the script(s) do I delete? I sent all 3 of my scripts I just need some guidance because I’m fairly new to scripting :sob:

delete this:

replicatedStorage.BestTime.OnServerEvent:Connect(function(player, time)
	time = math.floor(time * 1000)
	leaderboardStore:SetAsync(player.UserId, time)
	updateLeaderboard()
end)

I mean rename the “time” variable to something like “Time”. You can see the variable is blue, that means its interfering with roblox. Don’t stress and good luck on your coding adventure

1 Like

Preciate you but is that the only thing wrong in the code? It says I didn’t have any warnings but I know roblox is sensitive with capital letters and stuff

What doesn’t exactly work? Even if you type a letter wrong, it will spit out an error. Are you sure there aren’t any messages inside the output?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.