How would I display the left time for an audio?

Hello,
I’m currently trying to make an audio player. I have already managed to make the time position label, but I do not know how I would make a time remaining label. I have already tried:

-- song position and song length are defined
local minsPlayed = math.floor(songPosition/60)
local secsPlayed = math.floor(songPosition%60)
local minsFull = math.floor(songLength/60)
local secsFull = math.floor(songLength%60)

timeLeft.Text = "– "..string.format("%02d:%02d", minsFull - minsPlayed, secsFull - secsPlayed)

which just makes the seconds go into negative when they reach 0, even though minutes are still left.

You should perform the subtraction before dividing it into minutes and seconds:

local timeLeft = songLength - songPosition
local minsLeft = math.floor(timeLeft / 60)
local secsLeft = math.floor(timeLeft % 60)

timeLeftLabel.Text = "– " .. string.format("%02d:%02d", minsLeft, secsLeft)
1 Like