Issue with the music system

I’m creating a music system, and I do acknowledge that there are some tutorials for it, but I wanted to make one by myself rather than copying one. It works perfectly fine sometimes, commonly in studio, but not always. It does show the UI, but it doesn’t actually play the song either changes the rbxassetid://. What’s the issue here?

--[ Variables
local playlist = game:GetService("ReplicatedStorage"):WaitForChild("Playlist"):GetChildren()
local songObj = Instance.new("Sound", game.Workspace)
songObj.MaxDistance = "inf"
--[ Setup
repeat wait()
	for i = 1, #playlist do
		local nowPlaying = playlist[i]
		songObj.SoundId = nowPlaying.SoundId
		print(songObj.SoundId, songObj.IsPlaying)
		songObj:Play()
		game:GetService("ReplicatedStorage"):WaitForChild("Music"):FireAllClients(nowPlaying.SoundId)
		repeat wait() until not songObj.Playing
		wait(.3)
	end
until false

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Don’t believe you can do that, trying to assign a property of type number to a string will result in it becoming NaN and therefore 0. Instead of Parenting to Workspace, Parent to SoundService and Play it there without any issues.

Some issues I also spotted:


You use GetService for ReplicatedService twice, try to use a variable rather than getting it multiple times.


This can be heavily bad on performance, I’d recommend using the :Wait() method on an event such as soundObj.Ended or soundObj.Stopped as that’ll yield the thread until the event fires.

I removed the MaxDistance line, parented the music to SoundService, replaced repeat wait() until not songObj.Playing with soundObj.Ended:Wait(), but it only seems to play one song only.

Edit: it keeps printing that it’s playing, but I don’t hear anything.

All of that complex scripting is not needed. A simpler system I use is to get all the songs I want and insert them into workspace beforehand. Then set up a server script to just play them in order.
For this example I made a script simply with three songs in Workspace.

local Music1 = game.Workspace.Music1
local Music2 = game.Workspace.Music2
local Music3 = game.Workspace.Music3
--add more songs as required--
while true do
Music1:Play()
wait(Music1.TimeLength)
Music2:Play()
wait(Music2.TimeLength)
Music3:Play()
wait(Music3.TimeLength)
end 

I’m assuming that should do the trick. I know it can get pretty long if you have loads of songs on the list but as long as it works. I’m happy with it.

1 Like

Okay, placing Instance.new("Sound") in the for loop fixed it, thank you 2 for helping out!

1 Like