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()
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
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.
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()
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
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.