How do I code so that the radio can be turned on and off while shuffling music? (Songs are stored as separate audio assets in folder.)

Here’s the script in the workplace that controls how the radio works on the server. I just realized that the radio needs to be turned on or off based on a client, not on the whole server, but I still need help. Currently, while it does play and shuffle music, information about the current song does not show up on the radio GUI to the client, and the mechanic to turn the radio on and off doesn’t work.

local playlist = script.Parent.radioMusic:GetChildren()
print(playlist)
local n
local sound
local volume = 0
local SongsOnHold = {}

local musicEvent = game:GetService("ReplicatedStorage").RadioMusicEvent
local volumeEvent = game:GetService("ReplicatedStorage").RadioControlEvent
local ChoosingSong = true

function turnRadio(turnOn)
	if turnOn then
		volume = 0.5
	else
		volume = 0
	end	
end

volumeEvent.OnServerEvent:Connect(turnRadio)

function playAndSwitch()
	local CantPlay = true
	while CantPlay do
		wait(0.001)
		n = math.random(1,#playlist)
		sound = playlist[n]
		CantPlay = table.find(SongsOnHold, sound.Name)
	end
	ChoosingSong = false
	sound.TimePosition = 0
	musicEvent:FireAllClients(ChoosingSong, sound.Name, sound.SoundId)
	print("Sent request to change")
	sound:Play()
	sound.Ended:Wait()
	ChoosingSong = true
	musicEvent:FireAllClients(ChoosingSong, sound.Name, sound.SoundId)
	table.insert(SongsOnHold, sound.Name)
	print(SongsOnHold)
	if #SongsOnHold == 6 then
		table.remove(SongsOnHold, 1)
	end
	wait(0.001)
end

function soundControl()
	sound.Volume = volume
end

coroutine.wrap(playAndSwitch())
coroutine.wrap(soundControl())
1 Like

I don’t know much about coding but here’s some tips from what I’ve been able to read.

Create a client sided list on the same script that shows all details about the song, so it can be pulled easily.

To switch songs, I suggest that you stop the current song and start the next song after a very small wait.

Use a script that loads all music or waits for music to load. If there’s music that hasn’t loaded or disabled by the client, randomize again.

None of this may be useful, but that’s just what I know about basic music code.

Also replace that .001 wait, something like .01 should be easier to read by client if it’s slow?

I finally figured out. This is my LocalScript in the radio GUI:

local radioMusic = game:GetService("Workspace").radioMusic:GetChildren()
local open = false

function turnRadio()
	-- Credit: https://devforum.roblox.com/t/change-property-of-all-children-at-once/756103/21
	for _, song in ipairs(radioMusic) do
		if open then
			song.Volume = 0.5
		else
			song.Volume = 0
		end
	end
end
turnRadio() -- :>

local Tweeninfo = TweenInfo.new(0.001, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)

local button = script.Parent
local frame = button.Parent
frame.AnchorPoint = Vector2.new(0, 0)
frame.Position = UDim2.new(0, 0, 0, 0)
frame.Size = UDim2.new(0, 100, 0, 100)
frame.SongName.Visible = false
frame.ObtainIDLabel.Visible = false
frame.ObtainID.Visible = false

function radioHideAndSeek()
	if open then
		frame.SongName.Visible = true
		frame.ObtainIDLabel.Visible = true
		frame.ObtainID.Visible = true
	else
		frame.SongName.Visible = false
		frame.ObtainIDLabel.Visible = false
		frame.ObtainID.Visible = false
	end
end

function click()
	button.Selectable = false
	if open then
		open = false
		turnRadio()
		radioHideAndSeek()
		frame:TweenSizeAndPosition(UDim2.new(0, 100, 0, 100), UDim2.new(0, 0, 0, 0))
	else
		open = true
		turnRadio()
		frame:TweenSizeAndPosition(UDim2.new(0, 366, 0, 100), UDim2.new(-2.66, 0, 0, 0))
		radioHideAndSeek()
	end
	button.Selectable = true
end

script.Parent.MouseButton1Click:Connect(click)
2 Likes