How would I make a working gta-like radio station with multiple serversided channels/stations?

I’m making a GTA 5-like car radio for my game, but stumbled accross a problem that I don’t know how to solve. In the radiomenu there’s 3 different texts being displayed, the top one displaying
the radiochannel, the second displaying the artist and the last displaying the song title.

Radio_problem

When changing between the channels the same artist name and title name are displayed even though
changing the channel. I’ve come to the conclusion that the song which song that plays the first stays there til it’s done and then if another radio channel have changed song already the
menu displays the title and artist of that song instead. The toptext displaying the channelname works though.

Watch the video here:

Google Drive, video >CLICK HERE<

This is the top localscript in the “MusicOrder” folder. It basically changes the name of the song and artistname in the channel (as well as the name of the channel". This script is the one not functioning



local name = script.Parent.Parent.Parent.Song
local chl = script.Parent.Parent.Parent.Channel
local artist = script.Parent.Parent.Parent.Artist


local button = script.Parent.Parent

local playing = script.Parent.Parent.Parent.Parent.BillonPlaying.Value


local sound1 = script.Parent.Happening
local sound2 = script.Parent["Falling lights"]
local sound3 = script.Parent["Simple Beginning"]
local sound4 = script.Parent.Japan
local sound5 = script.Parent["My to you"]
local sound6 = script.Parent.Peace
local sound7 = script.Parent.Angelic

playing = false

chosen = false
chl.Text = "Nothing"
name.Text = "-"
artist.Text = "-"

button.MouseEnter:Connect(function()
	button.ring.Visible = true
end)

button.MouseLeave:Connect(function()
	button.ring.Visible = false
end)




button.MouseButton1Down:Connect(function()
	

	button.Popup:Play()
	
	
	if 
		playing == false
		and 
		script.Parent.Parent.Parent.Parent.BillonPlaying.Value == false
		
		and
		chl.Text == "Nothing" 
		
		and
		sound1.Volume == 0
		and
		sound2.Volume == 0
		and
		sound3.Volume == 0
		and
		sound4.Volume == 0
		and
		sound5.Volume == 0
		and
		sound6.Volume == 0
		and
		sound7.Volume == 0
		and
		chosen == false

	then
		playing = true
		chosen = true
		chl.Text = "Billon Ravers"

		button.Switch:Play()
		wait(1)
		
		sound1.Volume = 0.5
		sound2.Volume = 0.5
		sound3.Volume = 0.5
		sound4.Volume = 0.5
		sound5.Volume = 0.5
		sound6.Volume = 0.5
		sound7.Volume = 0.5



	elseif
		
		playing == true
		and

		chl.Text == "Billon Ravers"
		and
		sound1.Volume == 0.5
		and
		sound2.Volume == 0.5
		and
		sound3.Volume == 0.5
		and
		sound4.Volume == 0.5
		and
		sound5.Volume == 0.5
		and
		sound6.Volume == 0.5
		and
		sound7.Volume == 0.5
		and
		chosen == true then
		
		chl.Text = "Nothing"
		
		playing = false

		sound1.Volume = 0
		sound2.Volume = 0
		sound3.Volume = 0
		sound4.Volume = 0
		sound5.Volume = 0
		sound6.Volume = 0
		sound7.Volume = 0
		chosen = false

	end
end)

if chosen == true then

	button.ring.Visible = true
	
	
elseif chosen == false then
	button.ring.Visible = false
	chl.Text = "Nothing"
	name.Text = "-"
	artist.Text = "-"

end




	
	


**This is the second localscript where the sound starts playing, they play all the time even though the volume is 0 and not only when the channel they belong to is on, therefore it is like a radio and not just a playlist that pauses when changing radiostation. I’ve tried to make it change name after every song and it kind of works but doesn’t disappear, like I said, if you change channel. **

local name = script.Parent.Parent.Parent.Song
local chl = script.Parent.Parent.Parent.Channel
local artist = script.Parent.Parent.Parent.Artist

local button = script.Parent.Parent


local sound1 = script.Parent.Happening
local sound2 = script.Parent["Falling lights"]
local sound3 = script.Parent["Simple Beginning"]
local sound4 = script.Parent.Japan
local sound5 = script.Parent["My to you"]
local sound6 = script.Parent.Peace
local sound7 = script.Parent.Angelic


while true do


	sound1:Play()
	name.Text = "Happening"
	artist.Text = "No data"
	sound1.Ended:Wait()
	wait(2)
	sound2:Play()
	name.Text = "Falling Lights"
	artist.Text = "Painted Skies & Viewer"
	sound2.Ended:Wait()
	wait(2)
	sound3:Play()
	name.Text = "Simple Beginnings"
	artist.Text = "Fred V"
	sound3.Ended:Wait()
	wait(2)
	sound4:Play()
	name.Text = "カガミ"
	artist.Text = "Akiba"
	sound4.Ended:Wait()
	wait(2)
	sound5:Play()
	name.Text = "My Love To You"
	artist.Text = "A-live"
	sound5.Ended:Wait()
	wait(2)
	sound6:Play()
	name.Text = "inpeace"
	artist.Text = "sewers"
	sound6.Ended:Wait()
	wait(2)
	sound7:Play()
	name.Text = "Angelic"
	artist.Text = "Loretta"
	sound7.Ended:Wait()
	wait(2)

end

My goal with this project is to make a radio that plays the same audio for everyone (only if they’re on the same station). Right now the music starts locally when a player joins, meaning that a player that has been in the game for a longer time has a different song futher in the playlist than the person that has recently joined. I would wanna fix that in the future but I’m too scared I’m gonna break the whole thing.

I’m not so good at coding so I would appriciate if you could explain or maybe write some code down. If you need more information just comment and I’ll answer as soon as possible!

Sincerely, freak.

1 Like

Okay so I’m going to keep my solution to this very simple & easy to understand.
The issue you are facing is not how to play the audio, but how to organize the audio.

Firstly make a module/table on the server containing something like this:

local radioStation = {
	["Robloxia Station"] = {
		songs = {}-- put your id's here,
		serverTimeCode = 0,
		currentSong = ""
	}
}

This module script can have as many radio stations as you’d like.

Next you can make a script that shuffles the songs in the server:

local radioStations = {
	["Robloxia Station"] = {
		listeners = {},
		songs = {
			{
				channel = 'Robloxia Station',
				artist = 'artist -',
				songname = 'oof',
				songid = '',
			},
		},-- put your id's here,
		currentSong = ""
	}
}

-- create a sound to track each in server storage

local soundAssets = serverstorage.SoundAssets

-- shuffle the songs
local function randomSongForStation(station)
	local randomSong = station.songs[math.random(#station.songs)] -- picks a random song
	local newSound = Instance.New('Sound')
	newSound.Name = randomSong.channel
	newSound.Parent = soundAssets
	newSound.SoundId = randomSong.songid
	station.currentSong = randomSong
end

-- make sure to shuffle when ended
soundAssets.ChildAdded:Connect(function( child )
	if not child:IsA('Sound') then return end
	child:Play()
	for i,v in pairs(radioStations[child.Name].listeners) do -- fire all the listeners that the song changed
		remoteEvent:FireClient(game.Players[v], )
	end
	-- this is the shuffle mechanisim
	local listen
	listen = task.spawn(function()
		child.Ended:Wait()
		randomSongForStation(child.Name)
		child:Destroy()
		task.cancel(listen)
	end)
end)

-- tune in funtion

local function tuneInPlayer(player, station)
	local stationFromServer = radioStations[station]
	-- remove the player from any previous radio station
	for i,v in pairs(radioStations) do
		if v.listeners[player.Name] then
			v.listeners[player.Name] = nil
		end
	end
	-- add player to listeners
	table.insert(stationFromServer.listeners,player.Name)
	-- return the id and timestamp and current song data
	return soundAssets[station].SoundId,soundAssets[station].TimePosition, soundAssets[station].currentSong
end

remoteFunction.OnServerInvoke = tuneInPlayer

Now on the client invoke the server for the song

local songId, timePosition, artistData = remoteFunction:InvokeServer()

-- load the info into a sound and use :Play()
5 Likes

@Interactivated got it on point I think you just separate them using lists or dictionaries if you want your code to be more organized and concise. Have it choose from the list using math.ceil(math.random) and a variable to keep track of the last song played in the list. (So the variable would keep track of the previous iteration)

1 Like

Thank you so much! I’ll look into it when I have the time, got a lot in school these last weeks.

1 Like

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