I’ve got this little handy dandy GUI that plays music for my players. I’m just stuck on how to switch between songs of different genres. It should be easy, but I’m trying to find a solution to it.
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local MusicGui = PlayerGui:WaitForChild("MusicGui")
local Button = MusicGui:WaitForChild("Button")
local LofiActive = false
local RapActive = false
local EDMActive = false
local RnbActive = false
local MusicFolder = PlayerGui:WaitForChild("Music")
local LofiFolder = MusicFolder:WaitForChild("Lofi")
local RapFolder = MusicFolder:WaitForChild("Rap")
local EDMFolder = MusicFolder:WaitForChild("EDM")
local RnBFolder = MusicFolder:WaitForChild("R&B")
local function RandomizeMusic(SoundGroup)
--Add Total
local TotalNumber = #SoundGroup:GetChildren()
math.randomseed(tick())
local RandomNumber = math.random(1, TotalNumber)
for _, Sound in pairs(SoundGroup:GetChildren()) do
if Sound.Name == tostring(RandomNumber) then
Sound:Play()
return Sound
end
end
end
local function StopAllSongs()
for _, Sound in pairs(LofiFolder:GetChildren()) do
Sound:Stop()
Sound.TimePosition = 0
end
for _, Sound in pairs(RapFolder:GetChildren()) do
Sound:Stop()
Sound.TimePosition = 0
end
for _, Sound in pairs(EDMFolder:GetChildren()) do
Sound:Stop()
Sound.TimePosition = 0
end
for _, Sound in pairs(RnBFolder:GetChildren()) do
Sound:Stop()
Sound.TimePosition = 0
end
end
spawn(function()
while true do
if LofiActive then
local Sound = RandomizeMusic(LofiFolder)
while LofiActive do
if Sound.Stopped then
LofiActive = false
Sound.TimePosition = 0
else
Sound.Ended:Wait()
Sound.TimePosition = 0
break
end
end
elseif RapActive then
local Sound = RandomizeMusic(RapFolder)
while RapActive do
if Sound.Stopped then
RapActive = false
Sound.TimePosition = 0
else
Sound.Ended:Wait()
Sound.TimePosition = 0
break
end
end
elseif EDMActive then
local Sound = RandomizeMusic(EDMFolder)
while EDMActive do
if Sound.Stopped then
EDMActive = false
Sound.TimePosition = 0
else
Sound.Ended:Wait()
Sound.TimePosition = 0
break
end
end
elseif RnbActive then
local Sound = RandomizeMusic(RnBFolder)
while RnbActive do
if Sound.Stopped then
RnbActive = false
Sound.TimePosition = 0
else
Sound.Ended:Wait()
Sound.TimePosition = 0
break
end
end
end
wait()
end
end)
Button.Changed:Connect(function()
StopAllSongs()
if Button.Text == "Music: Lofi" then
LofiActive = true
RapActive = false
EDMActive = false
RnbActive = false
elseif Button.Text == "Music: Rap" then
LofiActive = false
RapActive = true
EDMActive = false
RnbActive = false
elseif Button.Text == "Music: EDM" then
LofiActive = false
RapActive = false
EDMActive = true
RnbActive = false
elseif Button.Text == "Music: R&B" then
LofiActive = false
RapActive = false
EDMActive = false
RnbActive = true
end
end)
Button.MouseButton1Click:Connect(function()
if Button.Text == "Music: Off" then
Button.Text = "Music: Lofi"
elseif Button.Text == "Music: Lofi" then
Button.Text = "Music: Rap"
elseif Button.Text == "Music: Rap" then
Button.Text = "Music: EDM"
elseif Button.Text == "Music: EDM" then
Button.Text = "Music: R&B"
elseif Button.Text == "Music: R&B" then
Button.Text = "Music: Off"
end
end)
Visual Representation:
EDIT: The songs switch, now I’m having trouble looping it back so that it can play a different song from the same genre.