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

Why isn't my music playing in game, but is playing in Studio??

I've tried using onPlayerAdded:connect()... blah blah blah.. nothing works.. I'm just trying to get a song to play, wait(200), rinse and repeat for multiple songs.


audio = game:GetService("SoundService")

	wait(5)
while true do
	audio.Sound.Volume = 0.25
	
	audio.WWII:Play()
	wait(200)
	audio.Sound:Play()
	wait(200)
end

This is my latest (Studio) working script.. all my scripts have the tendency to only work in Studio.

Thanks!

2 comments

0

Try adding this before the while loop: repeat wait() until audio.Sound.IsLoaded. Additionally, I would recommend you use the Sound's Ended event in place of wait(200)

0

This worked! Thanks You. I figured the initial wait(5) would be enough without sourcing a wait for the sound to load. THANKS!

1 answer

✓ Accepted answer

LIke cfire said, it's best to use an event that tells you when a song ended so you can just move to the next when it's done.

To use this, put a Folder in ReplicatedStorage and name it 'Sounds'. Put your sounds in there.

local audio_list = game:GetService('ReplicatedStorage'):WaitForChild('Sounds')
local all_sounds = audio_list:GetChildren()
local current_sound = 1
local max_sounds = #all_sounds
local playing = false

while true do
	-- if nothing is playing enter the scope.
	if not playing then 
		playing = true
		local audio = all_sounds[current_sound]
		print(audio, 'playing')
		audio.Volume = .25
		audio:Play()
		-- fire when audo is done playing
		this_con = audio.Ended:Connect(function()
			-- disconnect the event; we don't need it anymore
			this_con:disconnect()
			-- sets playing to false so we can choose a new song
			playing = false
			-- Moves to the first song if we're at the last song, else it moves 
			-- to the next song in the table
			current_sound = (current_sound + 1 > max_sounds and 1 or current_sound + 1)
		end)
	end
	wait(1)
end

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