How would I make a radio that players can select the song?

So I wanted to make a radio that plays songs for a game I am working on, but I also wanted player to change the song kind of like how odysseytohome did it in this video:

I have no idea how he did it, so if you have any ideas, please reply!

NOTE: I do not care about it showing the current song in the object text, nor do I care about the reverb.

You can use the .Triggered event of ProximityPrompt | Roblox Creator Documentation, and play a random song (if that’s what you want).

1 Like

I don’t want to play a random song each time, I want it to be in order, though, making a random song play would be easier. Do I just make a table of all the songs, then when the proximity prompt gets triggered, I just randomly pick a song inside the table and play it, then stop the song when it the proximity prompt gets picked? Or do I do something else?

You can have a number control the songs, based on its key inside the dictionary.
Here’s a small example:

local Number = 0

local Songs = {
	[1]	= 000,
	[2]	= 000
}

-- ProximityPrompt triggered:
Number += 1

-- Get the song you want to play:
Songs[Number]
1 Like

Does the script play the first song play again if you try to change the last song?

You can add a check that resets the number to 1 when the max number has been reached.


Does the script play the first song play again if you try to change the last song?

If you want to change to the last song, you can do Number -= 1 instead of Number += 1.
You should also check if the song is the first one, you can then set it to the last one if you want.

1 Like

I am not very good with tables, how would I check when the max number has been reached? Do I just check if the number variable is for example, 7, and if it is, then reset it to 1?

In that case since this is a dictionary, you need to set a limit yourself manually as you cannot get the highest / lowest integer with a loop or with the math.max / math.min functions.

In you want the songs in a specific order, you’ll need a dictionary.
Here’s an example:

local Number = 1

local Songs = {
	[1] = 123,
	[2]	= 456,
	[3]	= 789
}

local Min = 1
local Max = 3

if Number == Max then
	Number = Min
elseif Number == Min then
	Number = Max
end
1 Like

I got an error after testing out the script:
image

Script:

local Number = 1
local Sound = script.Parent.Parent.Parent.Sound

local Songs = {
	[1] = 6460316914,
	[2]	= 625562047,
	[3]	= 6083707440
}

local Min = 1
local Max = 3

if Number == Max then
	Number = Min
elseif Number == Min then
	Number = Max
end

script.Parent.Triggered:Connect(function()
	Number += 1
	Sound.SoundId = "rbxassetid://"..Songs[Number]
end)

EDIT: I did a few changes to the code, one of them being printing Songs[Number], and it just prints nil.

The check should be inside the function, it should look like this:

local Sound = script.Parent.Parent.Parent.Sound

local Songs = {
	[1] = 6460316914,
	[2]	= 625562047,
	[3]	= 6083707440
}

local Min = 1
local Max = 3
local Number = Min -- Changed so you don't have to type it twice everytime you change it.


script.Parent.Triggered:Connect(function()
	if Number == Max then
		Number = Min
	elseif Number == Min then
		Number = Max
	else
		Number += 1
	end
	
	Sound.SoundId = ('rbxassetid://%d'):format(Songs[Number]) 
	-- Just something I do to make it look cleaner. Change if wanted.
end)

EDIT: I did a few changes to the code, one of them being printing Songs[Number] , and it just prints nil.

The reason it might print nil is because the number doesn’t exist in the dictionary, because you didn’t implement the check correctly.

I got an error after testing out the script:

This should also be fixed now, as this would be the same as my comment above.

1 Like

It works, but it’s only playing 2 songs, not 3. No errors, it’s just not playing the second song.

EDIT: I added 4 songs, and it’s only playing the last song, and the first song.

That is because the number increments before playing, here’s a quick fix:

local Number = 0 -- was previously 1.
1 Like

The same thing happens, only the last and the first songs play.

I’ll do some testing, as I haven’t tested this.
I’ll be back in a few minutes with a update.

1 Like

I have to leave for a few hours, so if I don’t respond for a really long time, you know why, and when I do respond, please like to let me know that you read it.

The issue is:
Everytime Number == Min, it would set it to max, vice-versa

Here’s a fix for that (my logic didn’t add up at first, so here’s a stable version).

local Songs = {
	[1] = 6460316914,
	[2]	= 625562047,
	[3]	= 6083707440
}

local Number = 0 -- -- Because it'll increment before playing.
local Min = 0 -- Because it'll increment before playing.
local Max = 3


script.Parent.Triggered:Connect(function()
	if Number == Max then
		Number = Min
	end
	Number += 1
	
	Sound.SoundId = ('rbxassetid://%d'):format(Songs[Number])
end)

I have to leave for a few hours, so if I don’t respond for a really long time, you know why, and when I do respond, please like to let me know that you read it.

That’s alright, the code seems to be working just fine now. Enjoy!

1 Like

I am back, it took 3 hours in total, the script seems to be working really well! There are a few things that aren’t perfect, but to fix them takes very small amounts of tweaking, thank you!

1 Like

Glad I could help.

(avoiding char limit)

1 Like