Help my songs work and it goes by the list, but once it’s the last song the script breaks, it plays the first song then stops and then it just wont play that first song, but plays the second song, I want it to loop after the last song to play the first song, then every other song once again, to the last one, and then the first one once again after the last one forever
I have a folder in the workspace called songs, and that’s where the script where I’ve just mentioned is located in that folder, and I don’t want it local cause I want everyone in that server to hear that song that is playing in the server, and whenever a person leaves, or joins they hear that same song everyone else is listening to.
It might be because you’re setting the .Playing property to true, instead of using the :Play() method. And instead of calling wait() for the duration of the song, you can use the Ended event.
while true do -- parenthesis around the "true" are unnecessary, and generally discouraged in Lua (although you can still keep them if you want)
currentTrack.Value = "Sub Zero - Lil Uzi Vert"
l__Songs__1.SubZero:Play()
l__Songs__1.SubZero.Ended:Wait()
task.wait(0.1)
-- rest of the songs
end
I’m also just wondering why you’ve decided to name the l__Songs__1 variable so weirdly, any reasoning?
The reason this may be happening is because you’re using Sound.Playing instead of Sound:Play(), the function that’s meant to ‘start’ your audio track. You may also want to use the Sound.Ended event when the audio track ends, instead of waiting it’s TimeLength.
I would also suggest changing how your loop works to prevent errors that may arise.
Example:
local currentTrack = game.Workspace.Songs.CurrentTrack
local Tracks =
{
{Name = "Sub Zero - Lil Uzi Vert", Song = l__Songs__l:WaitForChild("SubZero")};
{Name = "Slow Motion - Trey Songz", Song = l__Songs__l:WaitForChild("SlowMotion")};
{Name = "Just For The Night - Chris Brown, Migos", Song = l__Songs__l:WaitForChild("JustForTheNight")
...
}
while true do
for _, Track in ipairs(Tracks) do
local Song = currentTrack.Song
currentTrack.Value = Track.Name
-- Play the song, then wait until it ends
Song:Play()
Song.Ended:Wait()
end
wait(.1)
end
I would suggest changing around how it works and even optimizing it (since it’s a rough example, there’s plenty of optimizations you can make).
Can you go to my other post too I need help with animations the GUI, but thank you I’ll try this, and I wanna use wait cause I don’t like sound.ended some audios include extra stuff.
local MusicTracks = {} -- // Tracks go here
coroutine.wrap(function()
while true do
local RandomIndex = math.random(1, #MusicTracks)
local MusicTrack = MusicTracks[RandomIndex]
Music.TimePosition = 0
Music.SoundId = "rbxassetid://" .. MusicTrack
Music:Play()
Music.Ended:Wait()
end
end)()
This is my approach to how I usually do music players. If you want the music to play in an specific order then go through each track one by one, like this.
local MusicTracks = {} -- // Tracks go here
coroutine.wrap(function()
local LastIndex = 0
while true do
if LastIndex == #MusicTracks then
LastIndex = 0
end
LastIndex += 1
local MusicTrack = MusicTracks[LastIndex]
Music.TimePosition = 0
Music.SoundId = "rbxassetid://" .. MusicTrack
Music:Play()
Music.Ended:Wait()
end
end)()
but I want everything listed so I’d rather do wait like 200 seconds or 199 cause I can shorten the song, I’d like it randomize that’s cool but- I want it to wait a certain time, and not music.ended
I see, well this it’s still pretty easy to code in that feature. (along with the song names)
(quick tip - You can have tables inside of tables)
example 1 - randomized tracks
local Music = Instance.new("Sound")
Music.Name = "ServerMusic"
Music.Parent = workspace
--[[
Set the track's wait time to nil if you want the full track to play
]]
local MusicTracks = {
[1] = {
Name = "Happy Adventure Music";
Id = "196131899";
WaitTime = 60 * 2;
}
}
coroutine.wrap(function()
while true do
local RandomIndex = math.random(1, #MusicTracks)
local MusicTrackData = MusicTracks[RandomIndex]
Music.TimePosition = 0
Music.SoundId = "rbxassetid://" .. MusicTrackData.Id
Music:Play()
print(MusicTrackData.Name)
if MusicTrackData.WaitTime then
task.wait(MusicTrackData.WaitTime)
else
Music.Ended:Wait()
end
end
end)()
example 2 - sorted tracks
local Music = Instance.new("Sound")
Music.Name = "ServerMusic"
Music.Parent = workspace
--[[
Set the track's wait time to nil if you want the full track to play
]]
local MusicTracks = {
[1] = {
Name = "Happy Adventure Music";
Id = "196131899";
WaitTime = 60 * 2;
}
}
coroutine.wrap(function()
local LastIndex = 0
while true do
if LastIndex == #MusicTracks then
LastIndex = 0
end
LastIndex += 1
local MusicTrackData = MusicTracks[LastIndex]
Music.TimePosition = 0
Music.SoundId = "rbxassetid://" .. MusicTrackData.Id
Music:Play()
print(MusicTrackData.Name)
if MusicTrackData.WaitTime then
task.wait(MusicTrackData.WaitTime)
else
Music.Ended:Wait()
end
end
end)()
k I’m using example 2 but how would I also make it so everytime the song changes, there’s a string value that changes named "currentTrack in a folder called songs in workspace, and it changes the string value to the name of the song in workspace.
If I am reading this correctly you’re asking how to change a string’s value to the song name.
Which if that IS the case then…
local StringValue = nil -- // Change this to the specified instance
local Music = Instance.new("Sound")
Music.Name = "ServerMusic"
Music.Parent = workspace
--[[
Set the track's wait time to nil if you want the full track to play
]]
local MusicTracks = {
[1] = {
Name = "Happy Adventure Music";
Id = "196131899";
WaitTime = 60 * 2;
}
}
coroutine.wrap(function()
local LastIndex = 0
while true do
if LastIndex == #MusicTracks then
LastIndex = 0
end
LastIndex += 1
local MusicTrackData = MusicTracks[LastIndex]
Music.TimePosition = 0
Music.SoundId = "rbxassetid://" .. MusicTrackData.Id
Music:Play()
if StringValue then
StringValue.Value = MusicTrackData.Name
end
if MusicTrackData.WaitTime then
task.wait(MusicTrackData.WaitTime)
else
Music.Ended:Wait()
end
end
end)()
alright cool I got the string value working now I’m trynna list all my songs and I’m getting an error so how would I add another song and like more songs I wanna add to that list
Here’s the script:
local Music = Instance.new("Sound")
Music.Name = "ServerMusic"
Music.Parent = workspace
local currentTrack = workspace.Songs.CurrentTrack
--[[
Set the track's wait time to nil if you want the full track to play
]]
local MusicTracks = {
[1] = {
Name = "Sub Zero";
Id = "1576032985";
WaitTime = 117,472 * 2;
Name = "Jocelyn Flores";
Id = "426082642";
WaitTime = 120.084 * 2;
}
}
coroutine.wrap(function()
local LastIndex = 0
while true do
if LastIndex == #MusicTracks then
LastIndex = 0
end
LastIndex += 1
local MusicTrackData = MusicTracks[LastIndex]
Music.TimePosition = 0
Music.SoundId = "rbxassetid://" .. MusicTrackData.Id
Music:Play()
if currentTrack then
currentTrack.Value = MusicTrackData.Name
end
if MusicTrackData.WaitTime then
task.wait(MusicTrackData.WaitTime)
else
Music.Ended:Wait()
end
end
end)()```
local rs = game:GetService("RunService")
local ss = game:GetService("SoundService")
local main = Instance.new("Sound", ss)
local songs = {
{ name = "impostor - sussy baka [MV]", id = "one hundred ninty nine"}
}
while true do
for index, m in next, songs do
print(("playing %"):format(m.name))
main.SoundId = ("rbxassetid://%s"):format(m.id)
main:Play()
main.Ended:Wait() --
end
rs.Heartbeat:Wait()
end
local Music = Instance.new("Sound")
Music.Name = "ServerMusic"
Music.Parent = workspace
local currentTrack = workspace.Songs.CurrentTrack
--[[
Set the track's wait time to nil if you want the full track to play
]]
-- // [Index] not required due ot lua doing it automatically
local MusicTracks = {
{
Name = "Sub Zero";
Id = "1576032985";
WaitTime = 117.472 * 2;
};
{ Name = "Jocelyn Flores";
Id = "426082642";
WaitTime = 120.084 * 2;
};
}
coroutine.wrap(function()
local LastIndex = 0
while true do
if LastIndex == #MusicTracks then
LastIndex = 0
end
LastIndex += 1
local MusicTrackData = MusicTracks[LastIndex]
Music.TimePosition = 0
Music.SoundId = "rbxassetid://" .. MusicTrackData.Id
Music:Play()
if currentTrack then
currentTrack.Value = MusicTrackData.Name
end
if MusicTrackData.WaitTime then
task.wait(MusicTrackData.WaitTime)
else
Music.Ended:Wait()
end
end
end)()
You’re getting an error due to have two songs indexed into one
didn’t even notice that from his previous code, plus it wouldn’t of had played the first song anyways due to the first table variables being overwritten