Help with sound script

Hello community I am working on a script a sound script for music, that when touching an object activates the music and when touching another object then deactivates the music of the other and puts another music. The problem is that it does not deactivate it and the new music is not played.

Script, Object 1:

local debounce = true

script.Parent.Touched:Connect(function(hit)
	if debounce == true then
		debounce = false
		local players = game:GetService("Players")
		local player = players:GetPlayerFromCharacter(hit.Parent)
		print('SoundINICIO1')
		if player then
			player.PlayerGui.SoundService['About Time']:Play()
			player.PlayerGui.SoundService['Aint It Strange b']:Stop()
			player.PlayerGui.SoundService['Do It (No Vox Fx)']:Stop()
			player.PlayerGui.SoundService['Final Score (a)']:Stop()
			player.PlayerGui.SoundService['Sun Massacre Instru']:Stop()
			player.PlayerGui.SoundService['Tiger Hearts']:Stop()
		end
		debounce = true
	end
end)

Script, Object 2:

local debounce = true

script.Parent.Touched:Connect(function(hit)
	if debounce == true then
		debounce = false
		local players = game:GetService("Players")
		local player = players:GetPlayerFromCharacter(hit.Parent)
		print('SoundClassic')
		if player then
			player.PlayerGui.SoundService['Aint It Strange b']:Stop()
			player.PlayerGui.SoundService['Do It (No Vox Fx)']:Stop()
			player.PlayerGui.SoundService['Final Score (a)']:Stop()
			player.PlayerGui.SoundService['About Time']:Stop()
			player.PlayerGui.SoundService['Tiger Hearts']:Stop()
			player.PlayerGui.SoundService['Sun Massacre Instru']:Play()
		end
		debounce = true
	end
end)

Firstly, there is no wait before setting debounce to true which means there’s no yielding which means it’s going to activated many times. Secondly, the way you’re stopping and playing a bit unoptimal and cluttered. Try this

local debounce = true

local music = "Sun Massacre Instru"

script.Parent.Touched:Connect(function(hit)
	if debounce == true then
		debounce = false
		local players = game:GetService("Players")
		local player = players:GetPlayerFromCharacter(hit.Parent)
		print('SoundClassic')
		if player then
			for i,v in pairs(player.PlayerGui.SoundService:GetChildren()) do
				v:Stop()
			end
			player.PlayerGui.SoundService[music]:Play()
		end
		wait(1)
		debounce = true
	end
end)

And to make it work for the other, just change the music variable. If it doesn’t work either then I think it would be best to put your music somewhere else rather than SoundService, perhaps in a folder located somewhere and just reference that instead

The first script worked, but for the second one nothing worked :frowning: I will try to place it somewhere else.

Odd, it should work, I think it may be the placing of the sounds, try putting them in a folder as I said and reference that folder instead

It’s really strange because it’s in StarterGui.

Perhaps try putting it in StarterPlayerScripts?

It worked for me, creating it in LocalScript form, but there is only one thing that does not work, it is that when I touch the same object, the music is played again.

LocalScript:

local debounce = true

local music = "AboutTime"

game:GetService('Workspace').SpawnsTeleportPlace.INICIO1.Touched:Connect(function(hit)
	if debounce == true then
		debounce = false
		local player = game:GetService("Players").LocalPlayer
		print('SoundInicio1')
		if player then
			for i,v in pairs(player.PlayerGui.SoundService:GetChildren()) do
				v:Stop()
			end
			player.PlayerGui.SoundService[music]:Play()
		end
		wait(1)
		debounce = true
	end
end)

Just check if the audio is playing or not

if not player.PlayerGui.SoundService[music].IsPlaying then
   player.PlayerGui.SoundService[music]:Play()
end
1 Like

Can this code be placed below v:Play()?

local debounce = true

local music = "AboutTime"

game:GetService('Workspace').SpawnsTeleportPlace.INICIO1.Touched:Connect(function(hit)
	if debounce == true then
		debounce = false
		local player = game:GetService("Players").LocalPlayer
		print('SoundInicio1')
		if player then
			for i,v in pairs(player.PlayerGui.SoundService:GetChildren()) do
				v:Stop()
			end
			if not player.PlayerGui.SoundService[music].IsPlaying then
				player.PlayerGui.SoundService[music]:Play()
			end
		end
		wait(1)
		debounce = true
	end
end)

its dont worked :(. sorry, Im Begginer

I believe the song is not playing because the for loop stops any song that is playing, including the one that needs to be played. To avoid that you need to add a statement check inside the for loop comparing the name of the song with the iterated song’s name.

Something like this should work:

for Index, Music in pairs(player.PlayerGui.SoundService:GetChildren()) do
	if Music.Name == music then
		print("skipped "..Music.Name.." because it is the current song")
		continue
	end
	Music:Stop()
end

Note: I changed i,v to Index, Music to avoid confusion and to make it clear which is which.
Index is the number of the currently iterated instance, and Music is the instance itself.

I would recommend you to give proper names to those variables everytime you ever use a for loop to iterate over the Children, Descendants or Ancestors of an instance.

Sidenote: continue skips the current iteration.

Almost forgot to mention, but I strongly recommend you to place whatever SoundService is to a more proper location like ReplicatedStorage or the actual SoundService service itself, since as far as I’m aware things such as music probably should not be in PlayerGui.