How would I get a name of the audio from a table?

So, I’ve made a table of sounds that when you click a button, it plays one of them. The script for that goes like this:

local sounds = {sound1, sound2, sound3, sound4,} -- and so on
	
sounds[math.random(1, #sounds)]:Play()

The thing is, when one of those sounds play, I want to get the name of it and transfer it over to the text of a TextLabel. How would I do that?

1 Like

You can use MarkplaceService’s GetProductInfo() method, pass the audio ID into it and it should return the name of it and more.

1 Like

I could see how this could help, but I have quite a couple audio IDs and I would not want to repeat code over and over and over just for one audio ID each piece of code, lol. Are there any other methods to get the names of the audios, or no?

This is what functions are for :wink:

local function getAudioName(id: number): string
   local success, output = pcall(function()
      -- return the product's info
      return game:GetService("MarketplaceService"):GetProductInfo(id)
   end)

   if success then
       return output.Name -- returns the item's name in the marketplace
   else
       warn(output) -- if there was an issue, this sends it to the console so you can see what the problem is
   end

   return "[ unknown ]" -- default name if the actual name can't be fetched
end

print(getAudioName(9112854440)) -- prints "Rain On Leaves 2 (SFX)"

You can send a web request, but that would be redundant since you can just use the aforementioned method. Other than these two, no.

So I’ve tried this out for a bit by changing the code a lot, but I can’t seem to make it stop “spamming” the TextLabel/output. What I mean by that is when you click the sound while another sound is playing, it “flickers”. (video attached)

Here’s the code I’ve changed so far:

local function getAudioName(id: number): string
		local success, output = pcall(function()
			
			return game:GetService("MarketplaceService"):GetProductInfo(id)
		end)

		if success then
			if script.Parent.Parent.onoff.Text == not output.Name then
				print("[ Name Already In ]")
			elseif script.Parent.Parent.onoff.Text == output.Name then
				script.Parent.Parent.onoff.Text = "" ..output.Name
			end
			print("" ..output.Name) -- returns the item's name in the marketplace
		else
			warn(output) -- if there was an issue, this sends it to the console so you can see what the problem is
		end

		 -- [return ""] default name if the actual name can't be fetched
	end
	
	if sound1.Playing == true then print(getAudioName(12222019)) elseif sound1.Playing == soundplaying then print("sound already playing") end
-- and so on........

I’ve also silently removed the return " [ Teh text isnt coming!!! ] " or whatever it said, as it just kept spamming the Output no matter what.

Here’s the video from said above:

You can see in the video that in the Output, once you spam click the button, it spams the same audio in there (which is probably because of the ClickDetector on the gui and the print("" ..output.Name) everytime you click! but im really dumb haha!)

If the sounds are already predetermined then you can include the names of the sounds inside of the “sounds” table.

local sounds = {
	{name, sound1},
	{name, sound2},
	{name, sound3}
}

local random = math.random(1, #sounds)
sounds[random][2]:Play()
print(sounds[random][1])
2 Likes

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