What do you want to achieve?
I’m trying to get the song name out of :GetChildren in a folder.
What is the issue?
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.
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.
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