Random music player rapidly changes songs

I made a system where a radio would randomly select and play songs and switch to a different one after one has finished. However, it is rapidly switching songs even though the song hasn’t finished yet.

Here’s my code:

wait(1)
while true do
	local Sounds = script:GetChildren()
	local queue = script.Parent.queue
	if queue:FindFirstChildOfClass('Sound') then
		local sound = queue:FindFirstChildOfClass('Sound'):Clone()
		sound.Parent = script.Parent.playing
		script.Parent.Parent.Radio.antenna.BillboardGui.TextLabel.Text = "Now Playing: "..sound:FindFirstChild('name').Value
		sound:Play()
		wait(sound.TimeLength)
		sound:Destroy()
	else
		local RandomIndex = math.random(1,#Sounds)
		local RandomSound = Sounds[RandomIndex]:Clone()
		RandomSound.Parent = script.Parent.playing
		script.Parent.Parent.Radio.antenna.BillboardGui.TextLabel.Text = "Now Playing: "..RandomSound.Name
		RandomSound:Play()
		wait(RandomSound.TimeLength)
		RandomSound:Destroy()
	end
end

I believe I had an issue similar to that, I think I solved it like this…

	repeat
		task.wait(1)
	until not sound.IsPlaying
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.