Hey everyone! I was creating a stopwatch which records your time from touching one part to another part, so I made a leaderboard which uses your best time to put up on the leaderboard.
The issue here is that when I run the game with API enabled I see in the output an arithmetic error. This error occurs as soon as I join the game which causes the script to stop working. I put these calculations in so it can actually be able to get formatted to be put up on the leaderboard.
I’ve tried removing the line of code and it did not work I also changed up the values of the equation but still nothing had changed, anyone know?
The error comes from the line, “local time = data.Value/1000”, which can be found in the “local function updateleaderboard()” block of code.
local dataStoreService = game:GetService("DataStoreService")
local leaderboardStore = dataStoreService:GetOrderedDataStore("MainLeaderboard")
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 LeaderboardPart = workspace:WaitForChild("LeaderBoard")
local surfacegui = LeaderboardPart.LeaderboardGui
local positionLabels = {
surfacegui:WaitForChild(1),
surfacegui:WaitForChild(2),
surfacegui:WaitForChild(3),
surfacegui:WaitForChild(4),
surfacegui:WaitForChild(5),
surfacegui:WaitForChild(6),
surfacegui:WaitForChild(7),
surfacegui:WaitForChild(8),
surfacegui:WaitForChild(9),
surfacegui:WaitForChild(10),
}
local function updateLeaderboard()
local leaderboardData = leaderboardStore:GetSortedAsync(false, 10)
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()