Trying to make a radio system

Hello!
I’m trying to make a car radio GUI but I’m having trouble. As I’m sure you’ll come to notice I am a terrible scripter. Apologies for the jumbled mess but I don’t know where to go from here

  • The folder contains other folders which contain Audios. Blues would be the blues radio station, classic would be the classical, and so on.

This script is the ‘Change’ script, and it’s supposed to detect when the player clicks the button and then change the station. It only works as a seperate script, not in the other script though.

music = script.Parent.Parent.Parent.Music
play = {}
local playlist = music.Mute
current = script.Parent.Parent.Parent.Current


script.Parent.MouseButton1Click:Connect(function()

if current.Value == "Mute" then
	current.Value = "Rock"
	local playlist = music.Rock
else if current.Value == "Rock" then
	current.Value = "Psyche"
		local playlist = music.Psyche	
else if current.Value == "Psyche" then
	current.Value = "Easy"
		local playlist = music.Easy
else if current.Value == "Easy" then
	current.Value = "Folk"
	local playlist = music.Folk
else if current.Value == "Folk" then
	current.Value = "Disco"
	local playlist = music.Disco
else if current.Value == "Disco" then
	current.Value = "Static"
		
	local playlist = music.Static
else if current.Value == "Static" then
	current.Value = "Classic"
	local playlist = music.Classic
else if current.Value == "Classic" then
	current.Value = "Blues"
	local playlist = music.Blues
else if current.Value == "Surf" then
	current.Value = "Surf"
	local playlist = music.Mute
								end
							end
						end
					end
				end
			end
							
		end
	end
end
	
end)

*This is the script ‘PlayStop’, it is intended to make the actual song turn on/off. There’s another button to mute but I’m fine scripting that.

local player = game.Players.LocalPlayer

music = script.Parent.Parent.Parent.Music
play = {}


function shuffleSongs()
	for i = 1, #songs, 1 do
		local index = math.random(1, #songs)
		local x = play[i]
		play[i] = play[index]
		play[index] = x

	end
end



isPlaying = script.Parent.Parent.Parent.isPlaying
shuffle = script.Parent.Parent.Parent.shuffle
current = script.Parent.Parent.Parent.Current


script.Parent.MouseButton1Click:Connect(function()
	
	if current.Value == "Mute" then songs = music.Mute:GetChildren()
	if current.Value == "Rock" then songs = music.Rock:GetChildren()
	if current.Value == "Psyche" then songs = music.Psyche:GetChildren()
	if current.Value == "Easy" then songs = music.Easy:GetChildren()
	if current.Value == "Folk" then songs = music.Folk:GetChildren()
	if current.Value == "Disco" then songs = music.Disco:GetChildren()
	if current.Value == "Static" then songs = music.Static:GetChildren()
	if current.Value == "Classic" then songs = music.Classic:GetChildren()
	if current.Value == "Blues" then songs = music.Blues:GetChildren()
	if current.Value == "Surf" then songs = music.Surf:GetChildren()


	for i, v in pairs(songs) do
		play[i] = v
	end


	local sound



		if isPlaying.Value == false then
			sound = nil
			if shuffle.Value == true then
				shuffleSongs()
			end
			isPlaying.Value = true
			for i, v in pairs(play) do
				if v and isPlaying.Value == true then
					sound = v:Clone()
					sound.Parent = player
					sound:Play()
					sound.Ended:Wait()
					sound:Destroy()
				end
			end
			isPlaying.Value = false
		end
		if isPlaying.Value == true then
			sound = nil
			if shuffle.Value == true then
				shuffleSongs()
			end
			isPlaying.Value = false
			player:FindFirstChildOfClass("Sound"):Destroy()
			wait(1)
			isPlaying.Value = true
			for i, v in pairs(play) do
				if v and isPlaying.Value == true then
					sound = v:Clone()
					sound.Parent = player
					sound:Play()
					sound.Ended:Wait()
					sound:Destroy()
				end
			end
			isPlaying.Value = false
		end



										end
									end
								end
							end
						end
					end
				end
			end
		end

	end



	
end)

1 Like

The local variable playlist is lost here. Try playlist = "Rock" instead, since you have a playlist variable above this code.

2 Likes

Your code is structured poorly, I’ll redo it for you:

local music = script.Parent.Parent.Parent.Music
local play = {}
local playlist = music.Mute
local current = script.Parent.Parent.Parent.Current


script.Parent.MouseButton1Click:Connect(function()

if current.Value == "Mute" then
	current.Value = "Rock"
	local playlist = music.Rock
elseif current.Value == "Rock" then
	current.Value = "Psyche"
		local playlist = music.Psyche	
elseif current.Value == "Psyche" then
	current.Value = "Easy"
		local playlist = music.Easy
elseif current.Value == "Easy" then
	current.Value = "Folk"
	local playlist = music.Folk
elseif current.Value == "Folk" then
	current.Value = "Disco"
	local playlist = music.Disco
else if current.Value == "Disco" then
	current.Value = "Static"
		
	local playlist = music.Static
elseif current.Value == "Static" then
	current.Value = "Classic"
	local playlist = music.Classic
elseif current.Value == "Classic" then
	current.Value = "Blues"
	local playlist = music.Blues
elseif current.Value == "Surf" then
	current.Value = "Surf"
	local playlist = music.Mute
								end
							end
						end
					end
				end
			end
							
		end
	end
end
	
end)

That’s a quick redo, you should be using capitalisation for variables and you should also be using indentations for your code, it’s fairly unreadable for the most part.

elseif is one word, not two as well, that’s what’s messing it up.

2 Likes

Thank you… Sorry for the headache!

1 Like

That’s alright. If you run into any issues let me know since I only looked over it quickly, there might be another underlying problem.

2 Likes

Why so many elseif, that’s a very unnecessary amount of them, you should instead have a List in an Array and use it to determine the index, something like this:

Array = {
    [1] = {"Mute", nil} -- have your code check if the second Value is nil to turn off
    [2] = {"Rock", music.Rock}
    [3] = {"Psyche", music.Psyche}
}

You get the point.

2 Likes

Using Cairo’s data storage method, here’s how I would rewrite the channel changing code:

local musicList = {}

-- Populate the music list so that each key represents a folder and its name
-- e.g. [1] = { Name = "Rock", Folder = music.Rock }
for _, folder in music:GetChildren() do
    table.insert(musicList, { Name = folder.Name, Folder = folder })
end

-- Returns the index of a song by its name.
-- e.g. [15] = { Name = "Blues"} . findMusic("Blues") -> 15
function findMusic(name: string): number?
    for i, v in musicList do
        if v.Name == name then
            return i
        end
    end
    return nil
end

mouseclick connection blah blah.... (function()
    local index = findMusic(current.Value) + 1
    if index > #musicList then
        index = 1 -- If we overflow, go back to song 1
    end
    local entry = musicList[index]
    current.Value = entry.Name
    playlist = entry.Folder
end)

Here, we store an array of music entries. Each entry contains:

  1. The name of the song
  2. The folder (playlist) it represents

Each entry is represented by an numeric index which we can use to find the entry that comes after it.

We create a find function which we can use to find the index of any song simply from its name.
So, if “Rock” is the fifth song, it would return 5.

Then, inside the click detector, we get the position of the current song. Lets say the currently selected song is Rock. We call the find function to get its index which is 5. Then, we add 1 to it to get 6.

Now, we find the sixth song, which let’s say is Blues. Now we can set the playlist to the folder of Blues and you can use it for whatever you want.

But what if the next index was outside of the array, i.e. it was the last element? Well, we somply use an if-statement to detect that and set the index to 1, the first element.

1 Like

You can easily shorten this to just:

index = (index % #musicList) + 1
2 Likes

Oh yeah, brain fart. Forgot about modulo operator.

1 Like

Its an uncommon method, so i wouldn’t blame you

1 Like