How to convert timer string back to number?

Hello Devforum! I am trying to make a timed leaderboard, but I am stuck on the issue of changing the player’s time back into seconds. I convert the player’s time with the line string.format("%02d:%05.2f", timer/min, timer%min). Now, I want to convert this time back into seconds. but I am not sure how. Please help.

Why not store the player’s time somewhere it can be easily accessed instead of performing a double conversion. For example, you can use attributes to document a player’s time:

-- Storing the player's time
local Player = game.Players.HonestJar
local playerTime = os.clock() -- However you originally calculate their time goes here

Player:SetAttribute("playerTime", playerTime)

-- Getting the player's time in another script
local playerTime = Player:GetAttribute("playerTime")
1 Like

I am not too sure about attributes, so it is not exactly what I was looking for. Thank you for the suggestion though.

This post did the exact opposite, it might help you figure it out by doing it the other way around hopefully:

If all else fails, write a DM to the person who solved that post @Uglypoe , and i’m sure they can help out.

Here is one solution utilizing string:split()

local timer = 100
local min = 60
local formattedTime = string.format("%02d:%05.2f", timer/min, timer%min)

print("Formatted Time:", formattedTime)

-- Extract time data from formatted time using string:split()

local timeData = formattedTime:split(":")
local minutes = timeData[1]
local seconds = timeData[2]

print("Player Time:", (minutes * min) + seconds, "seconds")

You will probably have to edit it using your own units/values. However, keep in mind that refactoring your code in order to not need to rely on extracting the time data from the formatted string is the best solution.

3 Likes