Is there a way to select a current playing track?

Hey, I was just wondering if there was a way specifically chose a current playing track that a player is playing just like you can do with animations. EG: something like :GetPlayingAnimationTracks() but for sounds.

local function getPlayingSounds()
	local playingSounds = {}
	for _, sound in ipairs(game:GetDescendants()) do
		local success, result = pcall(function()
			return sound:IsA("Sound")
		end)
		if success then
			if result then
				if result.IsPlaying then
					table.insert(playingSounds, result)
				end
			end
		else
			warn(result)
		end
	end
	return playingSounds
end

local playingSounds = getPlayingSounds()
for _, sound in ipairs(playingSounds) do
	print(sound.Name)
end

and a slightly more performant version.

local function getPlayingSounds()
	local playingSounds = {}
	for _, service in ipairs(game:GetChildren()) do
		local success, result = pcall(function()
			return service:GetDescendants()
		end)
		if success then
			if result then
				for _, sound in ipairs(result) do
					if sound:IsA("Sound") then
						if sound.IsPlaying then
							table.insert(playingSounds, sound)
						end
					end
				end
			end
		else
			warn(result)
		end
	end
	return playingSounds
end

local playingSounds = getPlayingSounds()
for _, sound in ipairs(playingSounds) do
	print(sound.Name)
end
1 Like