Music GUI Not Updating

  1. What do you want to achieve?
    I’m trying to get my music GUI to update when a new player joins because right now it’s only updating when the song changes but when a player first joins it just shows the placeholder text.

  2. What is the issue?
    Not updating when player joins the server.

  3. What solutions have you tried so far?
    I tried adding a while wait() and a while true loop but none of those worked.

Server Script:

local replicatedStorage = game:GetService("ReplicatedStorage")

local mainSound = Instance.new("Sound", workspace)
mainSound.Name = "Background Music"

local musicList = script.Songs:GetChildren()
local idList = {}

local musicEvent = replicatedStorage:WaitForChild("Music")

game.Players.PlayerAdded:Connect(function(Plr)
	Plr.CharacterAdded:Connect(function(Char)
		script["Music"]:Clone().Parent = Plr.PlayerGui
	end)
end)

wait(5)

if #musicList > 0 then
	for num,obj in pairs(musicList) do
		local currentID = obj.SoundId or nil
		if currentID then
			idList[num] = {ID = currentID, Artist = obj.Artist.Value, Name = obj.Name}
		end
	end
end

songNumber = 1

while wait() do
	mainSound.TimePosition = 0
	
	if #musicList > 0 then
		mainSound.SoundId = idList[songNumber].ID
		mainSound.Volume = 0.2
		mainSound:Play()
		
		musicEvent:FireAllClients(idList[songNumber].Artist, idList[songNumber].Name)
	end
	
	songNumber = songNumber + 1
	
	if songNumber > #musicList then
		songNumber = 1
	end
	
	mainSound.Ended:wait()
end

Local Script:

local replicatedStorage = game:GetService("ReplicatedStorage");
local musicEvent = replicatedStorage:WaitForChild("Music");

local artistName = script.Parent:WaitForChild("Artist");
local songName = script.Parent:WaitForChild("Song");

musicEvent.OnClientEvent:Connect(function(Artist, Name)
	if (Artist and Name) then
		artistName.Text = Artist
		songName.Text = Name
	end
end)
2 Likes

Add a player joined function.

game.Players.PlayerAdded:Connect(function(player)
-- do your firing here
end)

Do I add the function inside the while wait loop?

No. Heres the updated code:

local replicatedStorage = game:GetService("ReplicatedStorage")

local mainSound = Instance.new("Sound", workspace)
mainSound.Name = "Background Music"

local musicList = script.Songs:GetChildren()
local idList = {}

local musicEvent = replicatedStorage:WaitForChild("Music")

game.Players.PlayerAdded:Connect(function(Plr)
	Plr.CharacterAdded:Connect(function(Char)
		script["Music"]:Clone().Parent = Plr.PlayerGui
	end)
end)

wait(5)

if #musicList > 0 then
	for num,obj in pairs(musicList) do
		local currentID = obj.SoundId or nil
		if currentID then
			idList[num] = {ID = currentID, Artist = obj.Artist.Value, Name = obj.Name}
		end
	end
end

songNumber = 1

while wait() do
	mainSound.TimePosition = 0
	
	if #musicList > 0 then
		mainSound.SoundId = idList[songNumber].ID
		mainSound.Volume = 0.2
		mainSound:Play()
		
		musicEvent:FireAllClients(idList[songNumber].Artist, idList[songNumber].Name)
	end
	
	songNumber = songNumber + 1
	
	if songNumber > #musicList then
		songNumber = 1
	end
	
	mainSound.Ended:wait()
end

game.Players.PlayerAdded:Connect(function(player)
wait(3)
		musicEvent:FireClient(player,idList[songNumber].Artist, idList[songNumber].Name)
end)

It didn’t work. I don’t think lines of code work after a while wait loop

local replicatedStorage = game:GetService("ReplicatedStorage")

local mainSound = Instance.new("Sound", workspace)
mainSound.Name = "Background Music"

local musicList = script.Songs:GetChildren()
local idList = {}

local musicEvent = replicatedStorage:WaitForChild("Music")


wait(5)

if #musicList > 0 then
	for num,obj in pairs(musicList) do
		local currentID = obj.SoundId or nil
		if currentID then
			idList[num] = {ID = currentID, Artist = obj.Artist.Value, Name = obj.Name}
		end
	end
end


game.Players.PlayerAdded:Connect(function(Plr)
	Plr.CharacterAdded:Connect(function(Char)
		script["Music"]:Clone().Parent = Plr.PlayerGui
        muiscEvent:FireClient(Plr,idList[songNumber].Artist, idList[songNumber].Name)
	end)
end)
songNumber = 1

while wait() do
	mainSound.TimePosition = 0
	
	if #musicList > 0 then
		mainSound.SoundId = idList[songNumber].ID
		mainSound.Volume = 0.2
		mainSound:Play()
		
		musicEvent:FireAllClients(idList[songNumber].Artist, idList[songNumber].Name)
	end
	
	songNumber = songNumber + 1
	
	if songNumber > #musicList then
		songNumber = 1
	end
	
	mainSound.Ended:wait()
end

That updates it but it doesn’t show the current song

local replicatedStorage = game:GetService("ReplicatedStorage")

local mainSound = Instance.new("Sound", workspace)
mainSound.Name = "Background Music"

local musicList = script.Songs:GetChildren()
local idList = {}

local musicEvent = replicatedStorage:WaitForChild("Music")


wait(5)

if #musicList > 0 then
	for num,obj in pairs(musicList) do
		local currentID = obj.SoundId or nil
		if currentID then
			idList[num] = {ID = currentID, Artist = obj.Artist.Value, Name = obj.Name}
		end
	end
end

songNumber = 1

game.Players.PlayerAdded:Connect(function(Plr)
	Plr.CharacterAdded:Connect(function(Char)
		script["Music"]:Clone().Parent = Plr.PlayerGui
        muiscEvent:FireClient(Plr,idList[songNumber].Artist, idList[songNumber].Name)
	end)
end)

while wait() do
	mainSound.TimePosition = 0
	
	if #musicList > 0 then
		mainSound.SoundId = idList[songNumber].ID
		mainSound.Volume = 0.2
		mainSound:Play()
		
		musicEvent:FireAllClients(idList[songNumber].Artist, idList[songNumber].Name)
	end
	
	songNumber = songNumber + 1
	
	if songNumber > #musicList then
		songNumber = 1
	end
	
	mainSound.Ended:wait()
end

Okay so when I join it’s working but when another player joins it updates but it updates everyone to the next song instead of the current

Don’t know if this will fix that or not. Not sure what is causing that.

local replicatedStorage = game:GetService("ReplicatedStorage")

local mainSound = Instance.new("Sound", workspace)
mainSound.Name = "Background Music"

local musicList = script.Songs:GetChildren()
local idList = {}

local musicEvent = replicatedStorage:WaitForChild("Music")


wait(5)

if #musicList > 0 then
	for num,obj in pairs(musicList) do
		local currentID = obj.SoundId or nil
		if currentID then
			idList[num] = {ID = currentID, Artist = obj.Artist.Value, Name = obj.Name}
		end
	end
end

songNumber = 1

game.Players.PlayerAdded:Connect(function(Plr)
		script["Music"]:Clone().Parent = Plr.PlayerGui
        musicEvent:FireClient(Plr,idList[songNumber].Artist, idList[songNumber].Name)
end)

while wait() do
	mainSound.TimePosition = 0
	
	if #musicList > 0 then
		mainSound.SoundId = idList[songNumber].ID
		mainSound.Volume = 0.2
		mainSound:Play()
		
		musicEvent:FireAllClients(idList[songNumber].Artist, idList[songNumber].Name)
	end
	
	songNumber = songNumber + 1
	
	if songNumber > #musicList then
		songNumber = 1
	end
	
	mainSound.Ended:wait()
end

It didn’t do anything, still the same problem

You can probably figure it out if you mess around a bit.
I have to go, sorry.