So I am trying to create a GUI that has 4 radio stations in it. Each radio station has a list of Sound ID’s that I want it to play. When you click the “nextstation” or “previousstation” buttons, it goes to the next/previous station while all the songs within all of the stations are playing randomized and muted in the background unless you are on a chosen radio station.
The “Title” TextLabel changes to each station title as it should. However the problem is it only stays on the first radio station and it’s songs. When I try to click on the next/previous buttons nothing happens nor do the sounds change to the title’s corresponding songs for that radio station in the list.
Not sure what I am doing wrong but at least some of what I want is functioning as it should. Thanks for your assistance in advance.
local stations = {
[1] = {1847497222, 1841222553};
[2] = {1840499055, 9040380285};
[3] = {1844449787, 1836255687};
[4] = {9119677135, 135560679};
}
local minStations = 1
local maxStations = #stations
local curStation = 1
local currentSong = stations[curStation][math.random(2,#stations[curStation])]
script.Parent:WaitForChild("WELM").SoundId = "rbxassetid://" .. currentSong
script.Parent:WaitForChild("WRVN").SoundId = "rbxassetid://" .. currentSong
script.Parent:WaitForChild("KUSA").SoundId = "rbxassetid://" .. currentSong
script.Parent:WaitForChild("RETRO105").SoundId = "rbxassetid://" .. currentSong
script.Parent:WaitForChild("WELM"):Play()
script.Parent:WaitForChild("WRVN"):Play()
script.Parent:WaitForChild("KUSA"):Play()
script.Parent:WaitForChild("RETRO105"):Play()
function setMusic()
if curStation == 1 then
script.Parent:WaitForChild("StationName").Text = script.Parent:WaitForChild("WELM").Name
script.Parent:WaitForChild("WELM").Volume = 1
script.Parent:WaitForChild("WRVN").Volume = 0
script.Parent:WaitForChild("KUSA").Volume = 0
script.Parent:WaitForChild("RETRO105").Volume = 0
elseif curStation == 2 then
script.Parent:WaitForChild("StationName").Text = script.Parent:WaitForChild("WRVN").Name
script.Parent:WaitForChild("WELM").Volume = 0
script.Parent:WaitForChild("WRVN").Volume = 1
script.Parent:WaitForChild("KUSA").Volume = 0
script.Parent:WaitForChild("RETRO105").Volume = 0
elseif curStation == 3 then
script.Parent:WaitForChild("StationName").Text = script.Parent:WaitForChild("KUSA").Name
script.Parent:WaitForChild("WELM").Volume = 0
script.Parent:WaitForChild("WRVN").Volume = 0
script.Parent:WaitForChild("KUSA").Volume = 1
script.Parent:WaitForChild("RETRO105").Volume = 0
elseif curStation == 4 then
script.Parent:WaitForChild("StationName").Text = script.Parent:WaitForChild("RETRO105").Name
script.Parent:WaitForChild("WELM").Volume = 0
script.Parent:WaitForChild("WRVN").Volume = 0
script.Parent:WaitForChild("KUSA").Volume = 0
script.Parent:WaitForChild("RETRO105").Volume = 1
end
end
script.Parent:WaitForChild("NextStation").MouseButton1Click:Connect(function()
curStation = curStation +1
if (curStation > maxStations) then
curStation = minStations
end
setMusic()
end)
script.Parent:WaitForChild("PreviousStation").MouseButton1Click:Connect(function()
curStation = curStation -1
if (curStation < minStations) then
curStation = maxStations
end
setMusic()
end)
During the MouseButtonClick event, instead of using an if statement, you can use modulo operators to create a cycle-like system. Try this out and let me know if it works:
instead of having elseif for every station you could just loop through a stations folder that have all of the stations
-- Array of dictionaries containing the station name and tracks
local Stations = {
{["StationName"] = "WELM"; ["Tracks"] = {1847497222, 1841222553}};
{["StationName"] = "WRVN"; ["Tracks"] = {1840499055, 9040380285}};
{["StationName"] = "KUSA"; ["Tracks"] = {1844449787, 1836255687}};
{["StationName"] = "RETRO105"; ["Tracks"] = {9119677135, 135560679}};
}
-- Make varibles to avoid repitition
local Radio = script.Parent
local StationName = Radio:WaitForChild("StationName")
CurrentStation = 1 -- only keep track of the current station
local function ChangeMusic()
local CurrentStation = Stations[CurrentStation]
local CurrentSong = CurrentStation.Tracks[math.random(#CurrentStation.Tracks)]
-- loop through all of the other stations (sounds) in a Stations folder, and stop them
for _, Station in Radio.Stations:GetChildren() do
if Station.Name ~= CurrentStation.StationName then
Station:Stop()
continue
end
end
-- check if the current station is a real station
local Station = Radio.Stations:FindFirstChild(CurrentStation.StationName)
if not Station then warn("Couldn't find", CurrentStation.StationName, "in station list.") end
-- set the station info and play
Station.SoundId = "rbxassetid://".. CurrentSong
StationName.Text = CurrentStation.StationName
Station:Play()
end
local function Next()
CurrentStation += CurrentStation ~= #Stations and 1 or -(#Stations - 1)
ChangeMusic()
end
local function Previous()
CurrentStation = CurrentStation ~= 1 and CurrentStation - 1 or #Stations
ChangeMusic()
end
Radio:WaitForChild("NextStation").MouseButton1Click:Connect(Next)
Radio:WaitForChild("PreviousStation").MouseButton1Click:Connect(Previous)
This seems to have solved the issue. However, when you switch back and forth through the stations, the music restarts each time rather than continuing to play the song when you go back to the station, like above where the volume of all the stations except the station you are currently listening to is set to 0 rather than stopping them altogether. Any remedy to that in what you provided?
well this is normal behavior when listening to music
when you change the song, the time position should go to 0 because you just switched the song
if you don’t want this then you could replace Sound:Stop() & Sound:Play() with Sound:Pause() & Sound:Resume()
also i don’t recommend having sounds for every song
you should try using 1 sound
local Stations = {
{["StationName"] = "WELM"; ["Tracks"] = {1847497222, 1841222553}};
{["StationName"] = "WRVN"; ["Tracks"] = {1840499055, 9040380285}};
{["StationName"] = "KUSA"; ["Tracks"] = {1844449787, 1836255687}};
{["StationName"] = "RETRO105"; ["Tracks"] = {9119677135, 135560679}};
}
local Radio = script.Parent
local StationName = Radio:WaitForChild("StationName")
CurrentStation = 1
local function ChangeMusic()
local CurrentStation = Stations[CurrentStation]
local CurrentSong = CurrentStation.Tracks[math.random(#CurrentStation.Tracks)]
for _, Station in Radio.Stations:GetChildren() do
if Station.Name ~= CurrentStation.StationName then
Station:Pause() -- Pause instead of stop
continue
end
end
local Station = Radio.Stations:FindFirstChild(CurrentStation.StationName)
if not Station then warn("Couldn't find", CurrentStation.StationName, "in station list.") end
Station.SoundId = "rbxassetid://".. CurrentSong
StationName.Text = CurrentStation.StationName
Station:Resume() -- Resume instead of playing from beginning
end
local function Next()
CurrentStation += CurrentStation ~= #Stations and 1 or -(#Stations - 1)
ChangeMusic()
end
local function Previous()
CurrentStation = CurrentStation ~= 1 and CurrentStation - 1 or #Stations
ChangeMusic()
end
Radio:WaitForChild("NextStation").MouseButton1Click:Connect(Next)
Radio:WaitForChild("PreviousStation").MouseButton1Click:Connect(Previous)
Update: This doesn’t work with Station:Pause() and Station:Resume().
There has to be a feasible way so that when you go to the next or previous station the song gets changed to the current stations audio. However, in theory the songs for the other radio stations that you are not tuned into should still be played so that when you go back to the other stations, it will continue with those songs rather than restart the song/randomized queue.
This just crossed my mind as well, but would adding separate local functions help?
Such as:
Local function 1: Randomizes the picking of the next song in a radio station and plays it. Local function 2: Sets the chosen stations song volume to 1 when you hit the next/previous buttons Local function 3: Sets the other stations song volume to 0 when you hit the next/previous buttons Local function 4: Next button functions that corresponds to local function 3 & 4. Local function 5: Previous button functions that corresponds to local function 3 & 4.
Would this work or would this just be confusing/conflicting?