Songs not playing

Hello DevForum, I am working on a game similar to Club Iris that plays a selection of certain songs over and over again. For some reason, the first song plays fine, but after it ends the script won’t start another song. Can anyone help me figure out why?

local SoundIDS = {
	2685636911;
	5098298388;
	2916513007;
	866774692;
	531158940;
	3263711192;
	4754657595;
	4521908173;
	131396974;
	5069810459;
	513188679;
}

local LastUpdatedSong = 0
local Delay = 2
local MusicPlaying = false
local MusicStepped
MusicStepped = RunService.Stepped:Connect(function()
	if LastUpdatedSong + Delay < tick() and not MusicPlaying then
		LastUpdatedSong = tick()
		MusicPlaying = true
		
		Music.SoundId = "rbxassetid://".. tostring(SoundIDS[Random.new():NextInteger(1, #SoundIDS)])
		loadSound(Music)
		
		Music:Play()
		
		local MusicName = MarketplaceService:GetProductInfo(tonumber(Music.SoundId:sub(14)))
		print("Currently Playing: " .. tostring(MusicName.Name))
		print(Music.TimeLength)
		
		Music.Ended:Wait()
		MusicPlaying = false
		print'Ended'
	end
end)

It’s a bit unclear on how you are trying to handle playing different songs but I think you meant to use “or” instead of “and” at the if check:

Consider changing it to:
if LastUpdatedSong + Delay < tick() or not MusicPlaying then

Try changing the volume through the script.

Sound.Loaded

--from developer hub

local function loadSound(sound)
	-- Has the sound already loaded?
	if not sound.IsLoaded then 
		-- if not, wait until it has been
		sound.Loaded:Wait()
	end
end
loadSound(Workspace.Sound)

You getting any errors from this?

Why you need to use that complicated, I would personally use a for loop inside a while loop. And using the i value to determine which song to play.

local soundids = {
   123,
   456,
   789,
}
local music = script.Parent

while true do
	local rngid = soundids[math.random(1,#soundids)]
	music.SoundId = "rbxassetid://"..rngid
	music.TimePosition = 0
	music:Play()
        local musicname = game:GetService("MarketplaceService"):GetProductInfo(tonumber(rngid))
	print("Currently Playing: " .. tostring(musicname.Name))
	print(tostring(music.TimeLength))
	music.Ended:Wait(2)
end

tested, should work for you.

2 Likes

When it comes to longer songs Music.TimeLength seems to be way off. The Ended event didn’t fire until at least 20 seconds after.

When I make a sound system I just store all the songs in a folder and do this:

while true do
for i, song in pairs(SongFolder:GetChildren()) do
song:Play()
song.Ended:Wait()
end
end