TimeLength is equal to 0

Code:

local Songs = script.Parent:WaitForChild("Songs")

repeat task.wait() until Songs

local Holder = script.Parent:WaitForChild("Holder")

local SongName = Holder:WaitForChild("SongName")

local TimeStamp = Holder:WaitForChild("TimeStamp")

local ProgressBar = Holder:WaitForChild("SongProgress"):WaitForChild("Bar")

-- Start Song

while true do
	
	local ChosenSong = Songs:GetChildren()[math.random(1,#Songs:GetChildren())]

	SongName.Text = ChosenSong.Name
	
	ChosenSong:Play()
	
	local totalDuration = ChosenSong.TimeLength
	
	repeat
		
		local currentTimeStamp = ChosenSong.TimePosition
		
		local mins = math.floor(currentTimeStamp / 60)
		
		local secs = currentTimeStamp % 60
		
		TimeStamp.Text = string.format("%d:%02d", mins, secs) .. " - " .. string.format("%d:%02d", totalDuration, totalDuration)
		
		ProgressBar:TweenSize(UDim2.new(currentTimeStamp / totalDuration,0,1,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Sine,0.1)
		
		task.wait(1)
		
	until ChosenSong.TimePosition == totalDuration
	
	
	
	task.wait(1)
	
end

My song progress bar wont work bc the totalduration variable is 0 for some reason but the songs plays and the name displays

A couple things:

Using repeat task.wait() until Songs is pointless because you’re doing WaitForChild on it above. So that repeat line can go away.

The reason you’re having issues is because sound doesn’t stream in until you first play it (or you preload it). So, until it actually loads, the time length will be zero. I think you can fix this by using IsLoaded and Loaded on the sound object before entering your loop for the progress bar:

ChosenSong:Play()
if not ChosenSong.IsLoaded then
   ChosenSong.Loaded:Wait()
end

local totalDuration = ChosenSong.TimeLength

repeat ...

If that still doesn’t work, then you might need to do a little loop to wait until TimeLength isn’t 0 too.

1 Like

works but now my there is another issue

TimeStamp.Text = string.format("%d:%02d", mins, secs) .. " - " .. string.format("%d:%02d", totalDuration, totalDuration)

this line projects the total duration like 161:089

You’re not doing anything to convert totalDuration into a format of minutes and seconds there like you are with the current time. Also, I thought the format for padded zeros required a period in the format, such as %.2d instead of %02d.

You can solve this problem and improve your code this way

local Songs = script.Parent:WaitForChild("Songs")
local Holder = script.Parent:WaitForChild("Holder")
local SongName = Holder:WaitForChild("SongName")
local TimeStamp = Holder:WaitForChild("TimeStamp")
local ProgressBar = Holder:WaitForChild("SongProgress"):WaitForChild("Bar")

-- Function to update the song progress
local function updateSongProgress(song)
	local totalDuration = song.TimeLength
	while totalDuration == 0 do
		task.wait(0.1)
		totalDuration = song.TimeLength
	end
	while song.IsPlaying do
		local currentTimeStamp = song.TimePosition
		local mins = math.floor(currentTimeStamp / 60)
		local secs = currentTimeStamp % 60
		local formattedTotalDuration = math.floor(totalDuration)
		TimeStamp.Text = string.format("%d:%02d", mins, secs) .. " - " .. string.format("%d:%02d", formattedTotalDuration / 60, formattedTotalDuration % 60)
		ProgressBar:TweenSize(UDim2.new(currentTimeStamp / totalDuration, 0, 1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.1)
		task.wait(1)
	end
end

-- Start playing a random song
while true do
	local ChosenSong = Songs:GetChildren()[math.random(1, #Songs:GetChildren())]
	SongName.Text = ChosenSong.Name

	ChosenSong.Loaded:Wait()
	ChosenSong:Play()

	-- Update song progress in a separate task
	task.spawn(updateSongProgress, ChosenSong)

	-- Wait until the song finishes playing
	ChosenSong.Stopped:Wait()

	task.wait(1)
end
1 Like

mb just fixed it thank you for the help with the audio issue above :+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.