Music Script won't show song title

So I am currently working on a club hangout type game, and I am trying to get it where the song title shows up on screen more in the top bar. I got all of that set, but the issue I am running into is it won’t display the song title, I created it for when it cannot grab a song title, it says “Unknown”. What am I doing wrong here?

local SoundService = game:GetService("SoundService")
local AssetService = game:GetService("AssetService")
local SongDisplay = game:GetService("StarterGui"):WaitForChild("SongDisplay")
local Background = SongDisplay.Background
local Song = Background.Song

local songIds = {
	5410081471,
	5410080926
}

local currentIndex = 1

-- Shuffle function
local function shuffle(t)
	for i = #t, 2, -1 do
		local j = math.random(i)
		t[i], t[j] = t[j], t[i]
	end
end

shuffle(songIds) -- Shuffle the songs when the server starts

local function getSongTitle(id)
	local title = "Unknown" -- default title in case fetching fails
	local success, assetInfo = pcall(function()
		return AssetService:GetAssetInfoById(id)
	end)

	if success and assetInfo then
		title = assetInfo.Name
	end

	return title
end

local function playNextSong()
	if currentIndex > #songIds then
		return
	end

	local id = songIds[currentIndex]
	local title = getSongTitle(id)

	Song.Text = title

	local soundInstance = Instance.new("Sound")
	soundInstance.SoundId = "rbxassetid://" .. id
	soundInstance.Name = title
	soundInstance.Parent = SoundService

	soundInstance.Ended:Connect(function()
		soundInstance:Destroy()
		currentIndex = currentIndex + 1
		playNextSong()
	end)

	soundInstance:Play()
end

playNextSong()

It will play the music, but will not give me a title at all.

1 Like

Updating a ScreenGui in StarterGui will not update the interface for those who have already spawned (loaded their UI), this is because StarterGui and PlayerGui are two different things. A simple way to track the song name in a ScreenGui without using remotes would be to use either a StringValue or Attribute to store the song name.

Another issue would be that AssetService does not have the method GetAssetInfoById. We’ll change that too.

This is a simple example of how you could implement this:

Server Script

local SoundService = game:GetService("SoundService")
local MarketplaceService = game:GetService("MarketplaceService")

local songIds = {
	5410081471,
	5410080926
}

local currentIndex = 1

-- Shuffle function
local function shuffle(t)
	for i = #t, 2, -1 do
		local j = math.random(i)
		t[i], t[j] = t[j], t[i]
	end
end

shuffle(songIds) -- Shuffle the songs when the server starts

local function getSongTitle(id)
	local title = "Unknown" -- default title in case fetching fails
	local success, assetInfo = pcall(function()
		return MarketplaceService:GetProductInfo(id) -- fixed this
	end)

	if success and assetInfo then
		title = assetInfo.Name
	end

	return title
end

local function playNextSong()
	if currentIndex > #songIds then
		return
	end

	local id = songIds[currentIndex]
	local title = getSongTitle(id)

	-- we store the song name in workspace to auto replicate it to clients
	workspace:SetAttribute("CurrentSong", title)

	local soundInstance = Instance.new("Sound")
	soundInstance.SoundId = "rbxassetid://" .. id
	soundInstance.Name = title
	soundInstance.Parent = SoundService

	soundInstance.Ended:Connect(function()
		soundInstance:Destroy()
		currentIndex = currentIndex + 1
		playNextSong()
	end)

	soundInstance:Play()
end

playNextSong()

Local Script Inside ScreenGui

local SongScreenGui: ScreenGui = script.Parent
local SongTextLabel: TextLabel = SongScreenGui:WaitForChild("Background"):WaitForChild("Song")

local function updateSongName()
	songTextLabel.Text = workspace:GetAttribute("CurrentSong") or "Unknown Song"
end

updateSongName()
workspace:GetAttributeChangedSignal("CurrentSong"):Connect(updateSongName)
1 Like

Ah, now I realized what I was doing wrong, thank you so much for your input, however for the Local Script I kept getting an error (which it did still work) but I did a work around to get rid of that red underline from bashing my OCD against the wall.

Before:

local SongScreenGui: ScreenGui = script.Parent
local SongTextLabel: TextLabel = SongScreenGui:WaitForChild("Background"):WaitForChild("Song")

I then decided to do a type assertion so that way LUA understands the intent of what it’s doing. Basically in shorter form meaning “It works bro, trust me.”

After:

local SongScreenGui = script.Parent
local Background = SongScreenGui:WaitForChild("Background") :: Frame
local SongTextLabel = Background:WaitForChild("Song") :: TextLabel

But again, thank you so much for your input! :heart:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.