Hello! I’d appreciate any help with an issue that I am having on my current project! I’m new to scripting and I just cant get my desired outcome with this script. I’ve made various adjustments to the script watched a couple youtube videos and have done some research but I just can’t get it to work.
What I am trying to achieve is to have a music setlist initially playing as soon as a player joins the game, along with have a settings GUI that can be interacted with to adjust music volume. This UI will also have a list of songs that can be played individually (it stops the currently playing to play the selected song) – once the selected song finishes I would like the setlist to resume playing.
Currently I have a achieved a functional settings GUI/music menu. However, my problem is that I cannot get the initially playing setlist to play from the start, nor play after selecting a song from the menu.
The following is my Explorer hierarchy:
This the the current LocalScipt (“Music Control Script”) that has the functioning music menu but no starting audio:
local frame = script.Parent
local scrFrame = frame.ScrollingFrame
local music = frame.Music
local current = frame.Current
local volumeBar = frame.VolumeBar.VolumeGreen
local volumeUp = frame.VolumeUp
local volumeDown = frame.VolumeDown
local stop = frame.Stop
local musicButton = frame.Parent.MusicButton
local playing
frame.Visible = false
current.Text = "Now Playing:"
musicButton.MouseButton1Click:Connect(function()
frame.Visible = not frame.Visible
end)
local function playMusic(sound)
if playing then
playing:Stop()
end
sound:Play()
playing = sound
playing.Volume = tonumber(volumeBar.Text) / 100
end
volumeUp.MouseButton1Click:Connect(function()
if playing then
if playing.Volume < 1 then
playing.Volume += 0.1
end
volumeBar.Size = UDim2.new(playing.Volume, 0, 1, 0)
volumeBar.Text = math.floor(playing.Volume * 100 + 0.5)
end
end)
stop.MouseButton1Click:Connect(function()
if playing then
playing:Stop()
current.Text = "Now Playing:"
end
end)
volumeDown.MouseButton1Click:Connect(function()
if playing then
if playing.Volume > 0 then
playing.Volume -= 0.1
end
volumeBar.Size = UDim2.new(playing.Volume, 0, 1, 0)
volumeBar.Text = math.floor(playing.Volume * 100 + 0.5)
end
end)
for _, song in pairs(music:GetChildren()) do
local track = Instance.new("TextButton")
track.Parent = scrFrame
track.Text = song.Name
track.Font = Enum.Font.PatrickHand
track.TextScaled = true
track.MouseButton1Click:Connect(function()
current.Text = "Now Playing: "..track.Text
playMusic(music[track.Text])
end)
end
This is one of my adjusted scripts that plays audio when I join. (Though, not at the specified volume I set within the audio tracks themselves). This script also is preventing me from interacting with the music menu, so I could not tell if it would “resume” the setlist after playing a song from the menu:
local frame = script.Parent
local scrFrame = frame.ScrollingFrame
local music = frame.Music
local current = frame.Current
local volumeBar = frame.VolumeBar.VolumeGreen
local volumeUp = frame.VolumeUp
local volumeDown = frame.VolumeDown
local stop = frame.Stop
local playing
local musicList = {}
local isShuffling = true -- Flag to indicate shuffle mode
frame.Visible = false
current.Text = "Now Playing:"
-- Shuffle function
local function shuffle(array)
local counter = #array
while counter > 1 do
local index = math.random(counter)
array[counter], array[index] = array[index], array[counter]
counter = counter - 1
end
return array
end
-- Function to play music
local function playMusic(sound)
if playing then
playing:Stop()
end
sound:Play()
playing = sound
playing.Volume = tonumber(volumeBar.Text) / 100
-- If shuffle mode is on and there's more than one song left, shuffle the list and play the next song
if isShuffling and #musicList > 1 then
musicList = shuffle(musicList)
playMusic(music[musicList[1].Name])
end
end
-- Populate musicList with songs
for _, song in pairs(music:GetChildren()) do
table.insert(musicList, song)
end
-- Play the first song automatically
playMusic(musicList[1])
-- Handle music controls
volumeUp.MouseButton1Click:Connect(function()
if playing then
if playing.Volume < 1 then
playing.Volume = playing.Volume + 0.1 -- Corrected line
end
volumeBar.Size = UDim2.new(playing.Volume, 0, 1, 0)
volumeBar.Text = math.floor(playing.Volume * 100 + 0.5)
end
end)
stop.MouseButton1Click:Connect(function()
if playing then
playing:Stop()
current.Text = "Now Playing:"
end
end)
volumeDown.MouseButton1Click:Connect(function()
if playing then
if playing.Volume > 0 then
playing.Volume = playing.Volume - 0.1 -- Corrected line
end
volumeBar.Size = UDim2.new(playing.Volume, 0, 1, 0)
volumeBar.Text = math.floor(playing.Volume * 100 + 0.5)
end
end)
-- Add songs to the scrolling frame
for _, song in pairs(musicList) do
local track = Instance.new("TextButton")
track.Parent = scrFrame
track.Text = song.Name
track.Font = Enum.Font.PatrickHand
track.TextScaled = true
track.MouseButton1Click:Connect(function()
current.Text = "Now Playing: " .. track.Text
playMusic(music[track.Text])
end)
end
I have also tired one or two alternate options like having the setlist play inside of a folder separately & inside of the Workspace. The idea was that if the player is touching the specified part (which covers majority of the game) the music will play on shuffle. Then, separately, the LocalScript inside of the StarterGUI would pause the setlist if a player chooses to play a song from the music menu, and resume the setlist once the song was finished.
This was the hierarchy of the alternate option where the music plays in the Workspace:
(Area 2, was a WIP and didn’t affect anything)
And this was the “Music Script” that played the music initially:
local musicFolder = game.Workspace["Area 1"]["Lofi Area Music"] -- Adjusted to match the folder for the specific area
local musicTracks = musicFolder:GetChildren()
local shuffledPlaylist = {}
-- Function to shuffle the playlist
local function shufflePlaylist()
shuffledPlaylist = {}
local temp = {unpack(musicTracks)}
while #temp > 0 do
local index = math.random(1, #temp)
table.insert(shuffledPlaylist, temp[index])
table.remove(temp, index)
end
end
-- Function to play the shuffled playlist
local function playShuffledPlaylist()
for _, track in ipairs(shuffledPlaylist) do
local sound = Instance.new("Sound")
sound.SoundId = track.SoundId
sound.Volume = track.Volume or 1 -- Set default volume to 1 if not specified
sound.Parent = game.Workspace
sound:Play()
sound.Ended:Wait() -- Wait for the track to finish playing
sound:Destroy()
end
end
-- Shuffle the playlist initially
shufflePlaylist()
-- Check if music is already playing
local musicPlaying = false
-- Check if a player's character is within the defined region
local function checkPlayerInRegion(player)
local musicPart = game.Workspace["Area 1"]["Music Part"] -- Adjusted to match the name of your part
local character = player.Character
if character and musicPart then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local regionCheck = musicPart.Position - humanoidRootPart.Position
local distance = regionCheck.magnitude
if distance <= 10 then -- Adjust this value as needed to define the size of your region
if not musicPlaying then
return true
end
end
end
end
return false
end
-- Play the shuffled playlist when a player enters the region
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character.HumanoidRootPart.Touched:Connect(function()
if checkPlayerInRegion(player) then
musicPlaying = true
playShuffledPlaylist()
shufflePlaylist() -- Reshuffle the playlist for the next iteration
musicPlaying = false
end
end)
end)
end)
.
I apologize if this is lengthy, confusing, or incorrect to the guidlines, but I truly would appriciate any and all help/advice !!