i’m making a sort of delivery system that grades you based on how fast you deliver a package. i’ve seen people use both os.clock() and tick() for time-related uses, such as debounces and countdowns, and they both do approximately the same thing. the question is, which one should i use?
-- \\ grading criteria
local grades = {
{grade = "A+", threshold = 59},
{grade = "A", threshold = 119},
{grade = "B", threshold = 179},
{grade = "C", threshold = 239},
{grade = "D", threshold = 299},
{grade = "F", threshold = 359},
{grade = "DNP", threshold = 419},
}
--\\ timing variables
local deliveryStartTime = 0
local isTimingActive = false
--\\ timing functions
local function formatTime(seconds: number)
return tonumber(string.format("%.2f", seconds))
end
local function getGrade(score: number): string -- lower delivery time gives a higher score
for _, entry in grades do
if score <= entry.threshold then
return entry.grade
end
end
return "A+"
end
local function startDeliveryTimer()
if not isTimingActive then
deliveryStartTime = tick()
isTimingActive = true
end
end
local function stopDeliveryTimer()
if isTimingActive and deliveryStartTime > 0 then
local deliveryTime = formatTime(tick() - deliveryStartTime)
print(`completed delivery in {deliveryTime} seconds`)
deliveryStartTime = 0 -- reset the timer for the next delivery
isTimingActive = false
local grade = getGrade(deliveryTime)
print(grade)
end
end