Allow players to DJ a "radio station" which is broadcast to all players, but they can decide whether to mute or unmute

Hello,

My goal is to allow players to act as DJs in the game and work at an in-game radio station to decide which songs will be broadcast throughout the game to players. For simplicity, I have started with just trying to get an automated playlist to broadcast throughout the game. I would like the same music to be playing on all players computers, but I would like them to not hear that music unless they toggle on the radio option (and this option should only make the music audible for that player who clicked it).

I most recently tried placing the songs inside of a Sound Group within the StarterGui and then having a remote event fire to all clients that triggers the playing of the sound but with the volume set to 0. I then have a GUI with a localscript button that toggles the volume on and off. This would work sometimes, but at other times when I would load the game in studio it would end up playing some or all of the songs at the same time immediately (before I selected the radio option).

The code is simple and it is just a loop that randomly generates a number within the range of songs that exist, detects the sound ID’s length, plays that sound and unmutes it, waits the sound ID’s length time, re-mutes that sound and then moves onto the next sound.

NOTE: by default the below bool value is set to true this is just a placeholder so I can allow people to manually change the songs in the future
Here is the code:

while game.Workspace.RadioStationData.Automatic.Value == true do

local num = math.random(1,23) --last number must be the amount of songs in the folder

local sound = “Sound”…num

local songLength = game.StarterGui.MusicRadio[sound].TimeLength

game.ReplicatedStorage.RemoteEvents.PlayMusicRadio:FireAllClients(sound, songLength)

wait(songLength + 1)

end

A lesser issue is that I hear a “click” or “pop” whenever I stop playing the game in studio (which sounds like it is cutting off the audio abruptly when it quits the game, however this occurs when all of the audio sources are muted on the client.)

I have tried searching for assistance with this, but I was not able to find anything.

Thank you all very, very much!

2 Likes
while game.Workspace.RadioStationData.Automatic.Value == true do

local num = math.random(1,23) --last number must be the amount of songs in the folder

local sound = “Sound”…num

local songLength = game.StarterGui.MusicRadio[sound].TimeLength

game.ReplicatedStorage.RemoteEvents.PlayMusicRadio:FireAllClients(sound, songLength)

wait(songLength + 1)

end

Recommend putting your code in a code block. Just use x3 at the start and x3 at the end, but that was just some advice. So you would want to control the music playing from the server to make sure everyone has the same song playing, you could do this by firing an event to all clients every time a song changes.

Server:

local MusicChange = game:GetService("ReplicatedStorage").MusicEvent
local Rand = Random.new()
local Playlist = {1234567, 12345678, 123456789} -- // Put the ID's in here.
local LastSong = Playlist[Rand:NextInteger(1, #Playlist)]
local CurrentSong = LastSong

MusicChange:FireAllClients(CurrentSong)

MusicChange.OnServerEvent:Connect(function(SongFinished, LastId)
    if SongFinished == true then
        LastSong = LastId
        wait(0.1)
        CurrentSong = Playlist[Rand:NextInteger(1, #Playlist)]
        if CurrentSong == LastSong then
            repeat wait() CurrentSong = Playlist[Rand:NextInteger(1, #Playlist)] until CurrentSong ~= LastSong
            MusicChange:FireAllClients(CurrentSong)
        end
    end
end)

Client:

local MusicChange = game:GetService("ReplicatedStorage").MusicEvent

MusicChange.OnClientEvent:Connect(function(SongId)
    script.Music.SoundId = "rbxassetid://" .. SongId
    script.Music:Play()
    wait(script.Music.TimeLength + 0.05)
    MusicChange:FireServer(true, SongId)
end)

This code hasn’t been tested so please excuse me if it doesn’t work :smiley:

1 Like

Thank you SO much I am going to try these right now!

Does it work??

I am still testing it the game I was using it in kept crashing so I am putting it into a new studio file.

What do I do about this here? I am sorry I am still very new to scripting.

Sorry, my mistake. Try do

repeat wait() CurrentSong = Playlist[Rand:NextInteger(1, #Playlist)] until CurrentSong ~= LastSong

Thank you Tom_atoes you are so awesome thank you for helping me!!!

The pop happens because the song goes from audible to inaudible instantly.

Say this was your song:

image

As you can see, when you stop it abruptly like that, the waveform gets cut off, producing a pop. I don’t know the exact technical reasons as to why this happens, but to fix it, just try to fade it out instead of stopping it while it’s playing.

image

This would produce no pop as the waveform loses volume gradually instead of stopping abruptly.

Edit:

I only just realised that you said it only happens when they are muted. I do not know the reason as to why it happens when it’s muted. Are the songs playing, but at 0 volume? If so try it without them playing.

1 Like

Thank you! And I may have misspoke I apologize. I know that it happens when the song volumes are at 0, but I am not sure if it also happens (the click) when the songs are playing (however this would make sense). I will do more testing to get more information.

Hello everybody. This works in studio in Play Solo mode but it does not work if a network is simulated (the behavior doesn’t occur in this network simulated mode and I don’t see errors). Does anybody have any recommendations?

in the server script, replace this line:

MusicChange:FireAllClients(CurrentSong)

with these four lines:

game:GetService("Players").PlayerAdded:connect(function(player)
     repeat wait() until player.Character ~= nil
     MusicChange:FireClient(player, CurrentSong)
end)
3 Likes

Should be: game:GetService(“Players”).PlayerAdded:Connect(function(player)

connect is deprecated in favor of Connect, but I don’t know that there’s much of an actual difference at this point in time.

3 Likes

I think it is considerably pedantic to post a reply only to point out the connect vs Connect usage and nothing else directly related to the thread, I’d avoid doing so in future posts. (It’s fine to add it as a side note, just don’t make it your whole post)

5 Likes

Thank you all VERY much. I saw this after waking up and went to test it right away. This is the update:

While the script (as it was before Hax made his suggestion) did work in studio the other week, it no longer did when I tested it earlier this morning. I think it may be because of the recent update to studio. That being said, after making HaxHelper’s suggested change, it DID work in studio. Thank you very much Hax! As a side note, I tried it with the C in Connect capitalized and lowercase and it worked (in studio) in both cases.

(Edited addition:)

I have now tested it with the simulated network in studio and it works. I am uploading it to a place right now. If something works in simulated network mode (sorry if that is not the proper term) does it mean it will always work on the actual server or not necessarily?

It does work on the server, however after it played one song everything stopped.