Music Script troubles

Hi, so as a junior scripter I was interested in making a music script which shuffled songs through a table, however I have run into problems because after 4 - 6 songs depending on the server the script will stop playing music. What do I do to keep it playing. I tried putting it in a loop so that’s why it might look really ugly, and I was advised to use random.new instead of math.random by a friend. Any help would be appreciated

--Made by ToddSchofield
local sound = {
    5969688978,
    5169103361, 
    5702343773, 
    5702300971,
    1233658550, 
    5978783937,
    5983346950,
}

local music = Instance.new("Sound", workspace)
local marketplaceService = game:GetService("MarketplaceService")

while wait() do
    for _, v in ipairs(sound) do
	    for count = Random.new(tick()):NextInteger(1,7), #sound do
		    print(count)
		    music.SoundId = "rbxassetid://"..sound[count]
		    music.Loaded:Wait()
		    music.Volume = 0.5
		    music.TimePosition = 0
		    music.Name = "Ingame_Music"
		    wait(.5)
		    music:Play()
		    Asset = marketplaceService:GetProductInfo(sound[count])
		    print(Asset.AssetId, " : " , Asset.Name)
		    music.Ended:Wait()
	    end
    end    
end

Fixed … --Made by ToddSchofield
local sound = {5969688978,5169103361,5702343773,5702300971,1233658550,5978783937,5983346950

}

local music = Instance.new(“Sound”, workspace)
local marketplaceService = game:GetService(“MarketplaceService”)

while wait() do
for _, v in ipairs(sound) do
for count = Random.new(tick()):NextInteger(1,7), #sound do
print(count)
music.SoundId = “rbxassetid://”…sound[count]
music.Loaded:Wait()
music.Volume = 0.5
music.TimePosition = 0
music.Name = “Ingame_Music”
wait(.5)
music:Play()
Asset = marketplaceService:GetProductInfo(sound[count])
print(Asset.AssetId, " : " , Asset.Name)
music.Ended:Wait()
end
end
end

May I enquire as what I did wrong, just for learning purposes?

1 Like

The id’s where down the way it’s suppose to be across the way.

Here you go:

--Made by ToddSchofield
local sound = {
	5969688978,
	5169103361, 
	5702343773, 
	5702300971,
	1233658550, 
	5978783937,
	5983346950,
}

local music = Instance.new("Sound", workspace)
local marketplaceService = game:GetService("MarketplaceService")

while true do -- while wait() do is a clunky idiom; it doesn't affect your bug but I just removed it here
	-- no need for multiple for-loops here!
	-- get a random song between the indices in table sound:
	local count = Random.new():NextInteger(1, #sound)
	-- Random.new() doesn't need a parameter because the seed by default is "better" randomized than a math.random
	-- call
	print(count)
	music.SoundId = "rbxassetid://".. sound[count]
	if not music.IsLoaded then
		-- if not loaded, wait until it has been
		music.Loaded:Wait()
	end
	music.Volume = 0.5
	music.TimePosition = 0
	music.Name = "Ingame_Music"
	wait(.5)
	music:Play()
	Asset = marketplaceService:GetProductInfo(sound[count])
	print(Asset.AssetId, " : " , Asset.Name)
	music.Ended:Wait()  
end

Your issue (I believe) had to do with you using a Wait method call on the Loaded event when the music was already loaded, resulting in a rather difficult to trace infinite yield. I also did some tidying with your loops & use of the Random object.

Hopefully this solves your issue.