Unable To Get Song Name

  1. What do you want to achieve?
    I’m trying to get the song name out of :GetChildren in a folder.
  2. What is the issue?
    Capture2 Capture
  3. What solutions have you tried so far?
    I tried printing the name using musicList.Name but it prints a table value.
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] = currentID
		end
	end
end

songNumber = 1

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

musicList is a table, that contains all your sounds. You can for example print(musicList[1].Name) and that will print the name of the first song in the table.

This doesn’t change anything, you can do
script[“Songs”]:GetChildren() or script.Songs:GetChildren() and it does the same thing.

Next time if you don’t know how to help - simply don’t post anything.

First of all, musicList.Name would actually give the hexadecimal value of the table, and not the element. What you’d need to do is musicList[index].Name.

Slightly off topic, but I believe that there’s always a better alternative to using wait(), as you did in while wait() do.
Refer to Avoiding wait() and why for more info about this.

Correct me if I’m wrong, and I hope I helped!

How do I make it print when the song changes to 2 and the others?

I changed your code a little bit and added the print at the end (marked with a comment). Now you can put your song names wherever you want.

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, Name = obj.Name}
		end
	end
end

songNumber = 1

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

Thank you so much! That worked. Could I use the same process to get the artist name out of the song for the Gui? Capture3

Yes, change

idList[num] = {ID = currentID, Name = obj.Name}

to

idList[num] = {ID = currentID, Name = obj.Name, Artist = obj.Artist.Value}

and then you can get the artist from the idList like that:

print(idList[songnumber].Artist)

but remember to put artist in every sound object, otherwise we will have to modify the code a little bit.

Thank you so much for helping!

1 Like