How to get the full duration of a song?

I have a UI that displays the information of a song that you can play with another UI. I have 2 text labels and one displays how long the song has been playing for, but i have another one and i wanted to know if it was possible to have it display the entire length of a song. (Like if the song was 3:00 long, it would say that) Is this possible and if so, how would I do it? (I have a sound and then the song codes you input into a text box get played through that sound)

1 Like

Are you talking about Sound.Timelength?

https://developer.roblox.com/en-us/api-reference/property/Sound/TimeLength

1 Like

I tried to make a script for it but its not working:

local song = script.Parent.Parent.Parent.Parent.HomeScreen.Apps.Music.Background.Music

local songDuration = song.TimeLength

local function digital_format(n)
	return string.format("%d:%02d", math.floor(n/60), n%60)
end

while true do
	script.Parent.Text = digital_format(songDuration)
	wait()
end

Is there any sort of error or output? What do you mean by not working

Double check and make sure this correctly refers to the sound instance.

It’s just not saying the duration of the song it just says 0:00

Then that means it hasn’t loaded
You can combat this by writing

local song = script.Parent.Parent.Parent.Parent.HomeScreen.Apps.Music.Background.Music

local songDuration = song.TimeLength

local function digital_format(n)
	return string.format("%d:%02d", math.floor(n/60), n%60)
end

if not song.IsLoaded then
    song.Loaded:Wait();
end

while true do
	script.Parent.Text = digital_format(songDuration)
	wait()
end
1 Like

it still didn’t work it just says 0:00

I actually realized I wanted to do something slightly different with this so I had a script that got the full duration of the song and then counted down until it was over but it’s in seconds and I wanted to see if there was a way to convert this into a minutes and seconds format

I fixed this myself:

local sound = script.Parent.Parent.Parent.Parent.HomeScreen.Apps.Music.Background.Music

local function digital_format(n)
	return string.format("%d:%02d", math.floor(n/60), n%60)
end

while true do
	local goal = math.floor(sound.TimeLength - sound.TimePosition)
	script.Parent.Text = "-".. (digital_format(goal))
	wait()
end
3 Likes