How to get the Name of an object?

G’day. I created a script so that I can play background music in my game. Now I created a TextLabel that should display the Name of the Song that is currently playing. But I dont know how I should get the name of the song. Can someone help? Below is the script:

local SongsFolder = script['Songs']
local TextFrame = script.Parent.Parent:WaitForChild('ScreenGui'):WaitForChild('Frame'):WaitForChild('TextLabel') 

while wait() do
	for i,v in pairs(SongsFolder:GetChildren() ) do
		v:Play()
		v.Ended:Wait()
		v.TimePosition = 0
	end
end

TextFrame.TextLabel = "Now playing" --- Add the name of the song
wait(5)
TextFrame.TextLabel = ""

Bildschirmfoto 2024-01-07 um 12.44.46

1 Like

Simply store the sound instance in a local variable then show it’s name:

local SongsFolder = script['Songs']
local TextFrame = script.Parent.Parent:WaitForChild('ScreenGui'):WaitForChild('Frame'):WaitForChild('TextLabel') 
local songPlaying -- instance of playing song

task.spawn(function() -- prevents infinite loop from yielding code
while wait() do
	for i,v in pairs(SongsFolder:GetChildren() ) do
		songPlaying = v -- sets songplaying to v
		v:Play()
		v.Ended:Wait()
		v.TimePosition = 0
	end
end
end)

TextFrame.TextLabel = "Now playing "..songPlaying.Name --- Add the name of the song
wait(5)
TextFrame.TextLabel = ""
2 Likes

Alright, thanks. It should work when it is in StarterGUI, right? Since I just tried it in-game, but it isnt working.

1 Like

Yeah it should work, is there an error or nothing?

1 Like

There’s two ways of doing this.

  1. Renaming all the sounds to the title of their soundId, and just using the .Name property of that Sound object.

  2. Using MarketplaceService:GetProductInfo() to get the name automatically. This would look something like: (keep in mind SoundIds usually have “rbxassetid://” at the front, so you’d have to remove that and then use tonumber on the rest of the string to turn it into a number)

game:GetService("MarketplaceService"):GetProductInfo(1847254112).Name
1 Like

No need to remove rbxassetid:// you can string.sub(SoundId,14,string.len(Sound.id))

1 Like

In the console, it just says:
Stack Begin
Script: … (To lazy to write the whole script location)
Stack End

1 Like

Very helpful thank you. Go to view > output and enable that, then you can copy errors more easily. Or you can provide an image.

  12:54:59.813  Infinite yield possible on 'Players.TtvRaucra:WaitForChild("ScreenGui")'  -  Studio
  12:54:59.813  Stack Begin  -  Studio
  12:54:59.813  Script 'Players.TtvRaucra.PlayerGui.BackgroundMusic', Line 2  -  Studio - BackgroundMusic:2
  12:54:59.813  Stack End  -  Studio
  12:54:59.813  Infinite yield possible on 'Workspace.LightStorage:WaitForChild("EntranceZone")'  -  Studio
  12:54:59.813  Stack Begin  -  Studio
  12:54:59.813  Script 'Workspace.TtvRaucra.LightingOptimization', Line 36  -  Studio - LightingOptimization:36
  12:54:59.813  Stack End  -  Studio
  12:55:00.677  :: Adonis :: Requiring Alerts Module by ID; Expand for module URL >   ▶ {...}  -  Server

Not that, what’s above stack begin? Like above above.

Try removing a .Parent in that line, you are indexing player with ScreenGui, it should be the PlayerGui

1 Like

A small part of what is above it. All the Sound IDs that failed to load are not those who are in the Background Music Script by the way.

  12:54:53.647  Stack Begin  -  Studio
  12:54:53.647  Script 'Workspace.SCP.SCP-939.Animate', Line 14  -  Studio - Animate:14
  12:54:53.647  Stack End  -  Studio
  12:54:53.647  Infinite yield possible on 'Workspace.shelter_blast:WaitForChild("DoorOpened")'  -  Studio
  12:54:53.647  Stack Begin  -  Studio
  12:54:53.647  Script 'Workspace.Doors.shelter_blast.DetectCut', Line 1  -  Studio - DetectCut:1
  12:54:53.647  Stack End  -  Studio
  12:54:53.665  GroupID Morphs loaded.  -  Server - Attire handles:25
  12:54:53.682  Failed to load sound rbxassetid://5475286926: Unable to download sound data  -  Studio
  12:54:54.021  Animation Spoofer cannot run while game is running  -  Client
  12:54:55.008  Failed to load sound rbxassetid://5475286926: Unable to download sound data  -  Studio
  12:54:55.076  Requiring asset 6381249088.
Callstack:
cloud_6380577372.Tubemaker.Tubemaker, line 24
  -  Server
  12:54:55.512  Failed to load sound rbxassetid://9174711793: Unable to download sound data  -  Studio
  12:54:55.683  Failed to load animation - sanitized ID: rbxassetid://12933085934  -  Studio
  12:54:58.135  GroupID Morph System loaded.  -  Server
  12:54:58.136  GroupID Morphs completed.  -  Server
  12:54:58.209  require(assetId) cannot be called from a client.  assetId = 6381249088  -  Client
  12:54:58.209  Stack Begin  -  Studio
  12:54:58.209  Script 'cloud_6380577372.Tubemaker.Tubemaker', Line 24  -  Studio
  12:54:58.209  Stack End  -  Studio
  12:54:58.277  TransitArms  -  Server - TransitSystem:96
  12:54:58.281  TransitGate  -  Server - TransitSystem:97
  12:54:58.308  TransitArms  -  Server - TransitSystem:96
  12:54:58.309  TransitGate  -  Server - TransitSystem:97
  12:54:58.386   ▶ [SlaveClock][GetTime] - Slave clock is not yet synced (x8)  -  Client - SyncedTime:86
  12:54:58.452  Failed to load animation - sanitized ID: rbxassetid://11898686078  -  Studio
  12:54:58.553  PlayerLoader successful  -  Client - PlayerLoader:35
  12:54:58.588  Section four complete  -  Client - Menu System:76
  12:54:58.603  Failed to load animation - sanitized ID: rbxassetid://11898686078  -  Studio
  12:54:59.813  Infinite yield possible on 'Players.TtvRaucra:WaitForChild("ScreenGui")'  -  Studio

Okay, I think vector4_new solved your issue, I was lazy. By the way the error part should be red. Something more like this:
image

Still not working. I believe it could be because of my Main Menu but idk.

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