I am working on a song GUI that is designed with multiple features; a pause button, a way to change the volume, and a next button that is designed to skip the current iteration when pressed.
This is my code.
game.ReplicatedStorage.EnableSongGUI.OnClientEvent:Connect(function()
--Hopefully stop bugs:
wait(3)
--Defining variables:
local gui = script.Parent
local frame = gui.Frame
local songName = frame.SongName
local pause = frame.Pause
local setVol = frame.SetVol
local nextButton = frame.Next
--Needed for muting
local muted = false
--Needed for pausing
local paused = false
--Cycle through songs easily using a for loop
for count = 0, 5, 1 do
local skip = false
local songs = game.SoundService.Songs
--Get current song by using the current count:
local currentSong = songs[count]
--Original volume
local defVol = currentSong.Volume
--Play song
currentSong:Play()
--Make text for songName equal to the song's name, using a stringvalue
songName.Text = currentSong:WaitForChild("Name").Value
--Functions:
--For pause being clicked:
pause.MouseButton1Click:Connect(function()
--Pause if not paused, and resume if paused
if paused == false then
--Stop song
currentSong:Pause()
--Change text
pause.Text = "Resume"
--Paused is now true, meaning that the other part of the if statement will fire next time.
paused = true
else
--Resume song
currentSong:Resume()
--Change text
pause.Text = "Pause"
--Paused is now false, meaning that the other part of the if statement will fire next time.
paused = false
end
end)
--For volume button being clicked:
setVol.MouseButton1Click:Connect(function()
local volume = frame.Volume.Text
currentSong.Volume = tonumber(volume) / 100
print("Volume Change")
end)
--For next button being clicked:
nextButton.MouseButton1Click:Connect(function()
-- Skip to next iteration...
-- continue will not work here.
end)
--Wait
wait(currentSong.TimeLength)
end
end)
As the comment suggested, I cannot use continue, because it is nested in a function.
Any advice?
game.ReplicatedStorage.EnableSongGUI.OnClientEvent:Connect(function()
--Hopefully stop bugs:
wait(3)
--Defining variables:
local gui = script.Parent
local frame = gui.Frame
local songName = frame.SongName
local pause = frame.Pause
local setVol = frame.SetVol
local nextButton = frame.Next
--Needed for muting
local muted = false
--Needed for pausing
local paused = false
local count = 0
local songs = game.SoundService.Songs
local function playSong()
if count <= 5 then
local currentSong = songs[count]
--Original volume
local defVol = currentSong.Volume
--Play song
currentSong:Play()
--Make text for songName equal to the song's name, using a stringvalue
songName.Text = currentSong:WaitForChild("Name").Value
--Functions:
--For pause being clicked:
pause.MouseButton1Click:Connect(function()
--Pause if not paused, and resume if paused
if paused == false then
--Stop song
currentSong:Pause()
--Change text
pause.Text = "Resume"
--Paused is now true, meaning that the other part of the if statement will fire next time.
paused = true
else
--Resume song
currentSong:Resume()
--Change text
pause.Text = "Pause"
--Paused is now false, meaning that the other part of the if statement will fire next time.
paused = false
end
end)
--For volume button being clicked:
setVol.MouseButton1Click:Connect(function()
local volume = frame.Volume.Text
currentSong.Volume = tonumber(volume) / 100
print("Volume Change")
end)
--For next button being clicked:
nextButton.MouseButton1Click:Connect(function()
-- Skip to next iteration...
count = count + 1
playSong()
end)
--Wait
wait(currentSong.TimeLength)
count = count + 1
playSong()
end
end
playSong()
end)
I made the following changes in the script:
I created a local function playSong() that recursively calls itself after each song is played. This allows for skipping to the next song using the next button.I initialized the count variable outside of the for loop and incremented it inside the playSong() function to keep track of the current song index.I moved the songs variable declaration outside of the for loop to avoid redefining it multiple times.I removed the unnecessary skip variable as it was not used anywhere in the script.I removed the muted variable as it was not used anywhere in the script.
I can’t feel my fingers and with %100 chance my fixed script won’t work I’m sorry I’m not an expert scripter I’m sorry if it won’t work
Here is a restructured code sample, with an included muting function.
You can connect these events outside of the loop.
--= Roblox Services =--
local SoundService = game:GetService("SoundService")
--= Object References =--
local gui = script.Parent
local frame = gui:WaitForChild("Frame")
local songName = frame:WaitForChild("SongName")
local pause = frame:WaitForChild("Pause")
local setVol = frame:WaitForChild("SetVol")
local nextButton = frame:WaitForChild("Next")
local songs = SoundService:WaitForChild("Songs")
--= Variables =--
local muted = false
local paused = false
local currentSong: Sound
local volumeModifier = 1
local function startPlaying()
for _, song in songs:GetChildren() do
currentSong = song
currentSong.Volume = currentSong.Volume * volumeModifier
currentSong:Play()
songName.Text = currentSong:WaitForChild("Name").Value
currentSong.Ended:Wait()
end
end
local function skipSong()
currentSong.TimePosition = currentSong.TimeLength
end
local function pauseSong()
if paused == false then
currentSong:Pause()
pause.Text = "Resume"
paused = true
else
currentSong:Resume()
pause.Text = "Pause"
paused = false
end
end
local function muteSong()
if muted == false then
currentSong.Volume = 0
volumeModifier = 0
muted = true
else
currentSong.Volume = 1
volumeModifier = 1
muted = false
end
end
pause.MouseButton1Click:Connect(function()
pauseSong()
end)
setVol.MouseButton1Click:Connect(function()
local volume = frame.Volume.Text
currentSong.Volume = tonumber(volume) / 100
end)
nextButton.MouseButton1Click:Connect(function()
skipSong()
end)
game.ReplicatedStorage.EnableSongGUI.OnClientEvent:Connect(function()
startPlaying()
end)