Time Position not displaying in TextLabel

Hi,

I’m making a simple playlist and I wanted to display the Time Length and the Time Position
but the Time Position only display once. Here’s my Script:

local musicFolder = script.Parent.Musics

local songDetail = script.Parent.SongDetail
local nameArtist = songDetail.NamerArtist
local nameSong = songDetail.NamerSong
local songDuration = songDetail.SongDuration

local songDetail1 = script.Parent.SongDetail1
local songID = songDetail1.SongId

local RS = game:GetService("RunService")

for _, songs in pairs(musicFolder:GetChildren()) do
	if songs:IsA("Sound") then
		
		songs:GetPropertyChangedSignal("Playing"):Connect(function()
			if songs.Playing == true then
				nameSong.Text = songs.Name
			end
		end)
		
		songs:GetPropertyChangedSignal("Playing"):Connect(function()
			if songs.Playing == true then
				nameArtist.Text = songs.ArtistName.Value
			end
		end)
		-- This one vvv
		songs:GetPropertyChangedSignal("Playing"):Connect(function()
			if songs.Playing == true then
				RS.RenderStepped:Connect(function()
					songDuration.Text = math.floor(songs.TimePosition) .. " : " .. math.floor(songs.TimeLength)
				end)
			end
		end)
		
		songs:GetPropertyChangedSignal("Playing"):Connect(function()
			if songs.Playing == true then
				RS.RenderStepped:Connect(function()
					songID.Text = songs.SoundId
				end)
			end
		end)
		
	end
end

Playlist Local Script:

local Soundtracks = script.Parent.Musics:GetChildren()

function getRandomSong()
	local i = math.random(1, #Soundtracks)
	return Soundtracks[i]
end

function playSong()
	local Song = getRandomSong()
	Song:Play() 

	Song.Ended:Wait()
	playSong() 
end

playSong()

Thanks in Advance

I would simplify this into one single listener for efficiency.

local musicFolder = script.Parent.Musics

local songDetail = script.Parent.SongDetail
local nameArtist = songDetail.NamerArtist
local nameSong = songDetail.NamerSong
local songDuration = songDetail.SongDuration

local songDetail1 = script.Parent.SongDetail1
local songID = songDetail1.SongId

local RS = game:GetService("RunService")

for _, songs in pairs(musicFolder:GetChildren()) do
	if songs:IsA("Sound") then

		songs:GetPropertyChangedSignal("Playing"):Connect(function()
			if songs.Playing == true then
				nameSong.Text = songs.Name
				nameArtist.Text = songs.ArtistName.Value
				RS.RenderStepped:Connect(function()
					songDuration.Text = math.floor(songs.TimePosition) .. " : " .. math.floor(songs.TimeLength)
					songID.Text = songs.SoundId
				end)
			end
		end)

	end
end

If does not solve your issue, let me know :slight_smile:

2 Likes

It still doesn’t work when the other song shifts also I should put the script of the playlist just in case.

You should also wrap this part with another check to see if the song is playing. Or will try to continue even when the song isn’t playing.

				RS.RenderStepped:Connect(function()
					if(songs.Playing)then
						songDuration.Text = math.floor(songs.TimePosition) .. " : " .. math.floor(songs.TimeLength)
						songID.Text = songs.SoundId
					end
				end)

its possible this is causing an error that stops the script. Even if it doesn’t stop the script with an error. It may be overwriting the text label for the other songs.

1 Like

Consider @Emskipo’s reply, it might be useful.

1 Like

It works! Thanks for the help both of you

The shorter script can be changed to the following.

local folder = script.Parent
local soundFolder = folder:WaitForChild("Musics")
local sounds = soundFolder:GetChildren()

local function playSound()
	local sound = sounds[math.random(#sounds)]
	sound:Play()
	sound.Ended:Wait()
	task.wait(0.5)
	playSound() 
end
playSound()
1 Like
local mainFolder = script.Parent
local musicFolder = mainFolder.Musics
local songDetail = mainFolder.SongDetail
local nameArtist = songDetail.NamerArtist
local nameSong = songDetail.NamerSong
local songDuration = songDetail.SongDuration
local songDetail1 = mainFolder.SongDetail1
local songID = songDetail1.SongId

local RS = game:GetService("RunService")

for _, song in ipairs(musicFolder:GetChildren()) do
	if song:IsA("Sound") then
		song.Played:Connect(function(soundId)
			nameSong.Text = song.Name
			nameArtist.Text = song.ArtistName.Value
			songID.Text = soundId
			
			local connection do
				connection = RS.RenderStepped:Connect(function()
					if not song.IsPlaying then
						connection:Disconnect()
					end
					
					songDuration.Text = math.floor(song.TimePosition).." : "..math.floor(song.TimeLength)
				end)
			end
		end)
	end
end

https://developer.roblox.com/en-us/api-reference/event/Sound/Played

You should use the “.Played” event to detect when a sound instance is played, it also has the added benefit of its parameter representing the played sound’s ID.

1 Like