Sound .TimeLength not showing correct length?

So I’m making a system, that will choose a Radom song from a folder of songs, Now what happens is I clone a random song, parent it to workspace, and When I try to get the .TimeLength of the song it prints 0.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SongPool = ReplicatedStorage:WaitForChild("Songs"):GetChildren()

local StartSize = UDim2.new(0,0,1,0)
local FinishSize = UDim2.new(1,0,1,0)

local function digital_format(n) -- Converts seconds into a mm:ss timer
	return string.format("%d:%02d", math.floor(n/60), n%60)
end

while true do
    -- Start Here
	local CurrentSong = SongPool[math.random(#SongPool)]:Clone(); CurrentSong.Parent = game.Workspace.Song
	local SongsName, SongDuration = CurrentSong.Name, CurrentSong.TimeLength -- Here is where we get the Time.Length
	CurrentSong:Play()
	-- End Here
	
    -- Prints: 0:00 and 0
	print("Song Duration: "..digital_format(SongDuration).. " Before Function: " ..SongDuration) -- Prints 0:00 and 0
	
	
	script.Parent.Bar.Bar.Size = StartSize
	
	script.Parent.SongName.Text = SongsName
	script.Parent.MaxDuration.Text = digital_format(SongDuration)

	
	script.Parent.Bar.Bar:TweenSize(FinishSize, Enum.EasingDirection.InOut,Enum.EasingStyle.Linear, SongDuration)
	local duration = 0
	repeat
		duration = duration + 1
		script.Parent.Duration.Text = digital_format(duration)
		wait(1)
	until duration == math.round(SongDuration)
	duration = 0
	
end

More info: The song before it is cloned has a TimeLength of 160.522. This is also a LocalScript. The song also plays, and there are no errors in the output

So what I’m trying to figure out why the .TimeLength outputs 0?

Thanks In Advanced :smiley:

1 Like

I had several problems with the sounds, you could try using this before taking the duration time

CurrentSong.Loaded:Wait()

-- or

if not CurrentSong.isLoaded then
	CurrentSong.Loaded:wait()
end

If it doesn’t work you could try to take the duration time after starting the song, maybe you can use: CurrentSong.Playing or CurrentSong.IsPlaying

3 Likes

Thank you, It solved the problem. :smiley:

1 Like