So I have been trying to make a live FM Radio UI that has 4 radio stations and plays a set of songs for each station randomly.
The problem is that when I hit the next/previous buttons to change the stations it does not play the song as if it was playing live. Instead as the script currently shows below, it plays the song from the beginning or changes to the next random song and plays that.
I am looking for some guidance on how to make it so when you hit the previous/next buttons that it continues to play the songs for the stations as if it was live but have them muted unless you are listening to that current station.
-- 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)