Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this pick 10 different songs, then plays one?

This should pick ONE song, and then play that ONE song, but instead it loops for like 10 songs and plays then for a second then PLAYS a normal song, why?

while wait() do
		local pickSong = songs[math.random(1, #songs)]
		
		local sound = Instance.new("Sound", script.Parent)
		local songName = "$$"
			
		local asset = game:GetService("MarketplaceService"):GetProductInfo(pickSong)
		songName = asset.Name		
			
		remotes.Events.PlaySong:FireAllClients(pickSong, songName) 
		--print"YES"
		sound.SoundId = "rbxassetid://"..pickSong
		wait(sound.TimeLength)
	end

1 answer

✓ Accepted answer

It takes a little bit to find out the TimeLength of a Sound if it has not been loaded before, so it just returns 0 as the TimeLength.

Try using PreloadAsset to load the Sounds before they are played to prevent this issue.

local ContentProvider = Game:GetService("ContentProvider")
ContentProvider:Preload("rbxassetid://121321") --Preload whatever asset(s) you need.

Remember to wait for all the assets to load too, using the RequestQueueSize property.

local function WaitForAssetsToLoad()
	-- Yields until all requested assets are loaded.
	while (ContentProvider.RequestQueueSize > 0) do
		wait()
	end
end
WaitForAssetsToLoad()

This discussion is preserved as it was. New answers, comments, and votes are closed.