I’m currently working on making a Music System for my game which includes music requests.
The issue is: when a player requests a song to be played, it plays it correctly but when the song ends it doesn’t continue to play the default songs on the Configuration script. How would I make it so it continues to play the default songs once the requested song has finished playing?
Thank you in advance for your assistance!
LocalScript:
local Configuration = require(workspace.Configuration)
local function Alert(SongName)
pcall(function()
game.StarterGui:SetCore("SendNotification", {
Title = "Music System",
Text = SongName,
Icon = '',
Duration = 10
})
end)
end
local BlacklistedMusicIds = Configuration.BlacklistedMusicIds
script.Parent.MouseButton1Click:Connect(function()
local SoundId = script.Parent.Parent.TextBox
local SoundInfo = game.MarketplaceService:GetProductInfo(SoundId.Text)
local Name = SoundInfo.Name
Alert('Now Playing: '..Name)
workspace.MusicSound.SoundId = 'rbxassetid://'..SoundId.Text
workspace.MusicSound:Play()
script.Parent.Parent.Parent.RequestDone.Visible = false
end)
local AlertMusic = game.ReplicatedStorage.MuiscAlert
local MusicPlayer = workspace.MusicSound
local ConfigModule = require(workspace.Configuration)
local SoundIds = ConfigModule.Music_Ids
local MusicData = SoundIds[math.random(1,#SoundIds)]
while wait() do
MusicPlayer:Stop()
if not MusicPlayer.IsPlaying then
local sound = MusicData
local assetId = game:GetService('MarketplaceService'):GetProductInfo(sound)
MusicPlayer.SoundId = 'rbxassetid://'..sound
MusicPlayer:Play()
AlertMusic:FireAllClients('Now Playing: '..assetId.Name)
wait(1)
wait((MusicPlayer.TimeLength)-1)
end
end
instead of doing wait((MusicPlayer.TimeLength)-1), you could just use MusicPlayer.Ended:Wait() as it fires right after the sound ends.
About your problem, your MusicData doesn’t change everytime a music ends, so you could instead do it like this :
local AlertMusic = game.ReplicatedStorage.MuiscAlert
local MusicPlayer = workspace.MusicSound
local ConfigModule = require(workspace.Configuration)
local SoundIds = ConfigModule.Music_Ids
while wait() do
if not MusicPlayer.IsPlaying then
local sound = SoundIds[math.random(1,#SoundIds)]
local assetId = game:GetService('MarketplaceService'):GetProductInfo(sound)
MusicPlayer.SoundId = 'rbxassetid://'..sound
MusicPlayer:Play()
AlertMusic:FireAllClients('Now Playing: '..assetId.Name)
MusicPlayer.Ended:Wait()
end
end
Hmm, this is working, but it hasn’t fixed the issue. I’m noticing the Playing property of the MusicSound under Workspace is still true (checked) once the requested song ends. Would this be an issue?
A better way of doing this is by using Remote Events, therefore you can make a Remote Event in workspace called “RequestMusic”, when a music gets requested, the server changes the sound id, you could also do music queues for something like this so that the current song doesn’t get skipped right away. So here’s my solution for you :
Server Script :-
local AlertMusic = game.ReplicatedStorage.AlertMusic
local MusicPlayer = workspace.MusicSound
local ConfigModule = require(workspace.Configuration)
local SoundIds = ConfigModule.Music_Ids
local RequestMusicRemoteEvent = workspace.RequestMusic
local DefaultMusics = {} -- This is the default musics sound ids that will play if there's nothing on the music queue.
local MusicQueue = {} -- Queue for all player requested sound ids.
RequestMusicRemoteEvent.OnServerEvent:Connect(function(Id)
table.insert(MusicQueue, 1, Id)
end)
while true do
local NextMusic = MusicQueue[1] or DefaultMusics[math.random(1, #DefaultMusics)
if NextMusic then
local SoundId = NextMusic
local SoundInfo = game:GetService('MarketplaceService'):GetProductInfo(SoundId)
MusicPlayer.SoundId = 'rbxassetid://'..(SoundId)
MusicPlayer:Play()
table.remove(MusicQueue, 1)
AlertMusic:FireAllClients('Now Playing: '..(SoundInfo.Name))
MusicPlayer.Ended:Wait()
end
end
and this is the local script :-
local Configuration = require(workspace.Configuration)
local function Alert(SongName)
pcall(function()
game.StarterGui:SetCore("SendNotification", {
Title = "Music System",
Text = SongName,
Icon = '',
Duration = 10
})
end)
end
local BlacklistedMusicIds = Configuration.BlacklistedMusicIds
local TextBox = nil -- Replace this with the TextBox
local Button = nil -- Replace this with the Button
Button.Activated:Connect(function()
workspace.RequestMusic:FireServer(TextBox.Text)
end)
workspace.AlertMusic.OnClientEvent:Connect(function(Text)
Alert(Text)
end)
(This is untested meaning you’d have to test it first.)