Sound repeating after being muted

I’ve made a system where when you press a button it mutes the sound, but since the sound is looped so It’ll play once the second loop starts. I don’t want this to happen. Let me know if you need any more information.

The script:

local part = game.Workspace.SoundRegions2.Part
local part2 = game.Workspace.SoundRegions2.Part2
local part3 = game.Workspace.SoundRegions2.Part3
local part4 = game.Workspace.SoundRegions2.Part4
local part5 = game.Workspace.SoundRegions2.Part5
local part6 = game.Workspace.SoundRegions2.Part6

script.Parent.MouseButton1Click:Connect(function()
		if part.Volume == 0 or part2.Volume == 0 or part3.Volume == 0 or part4.Volume == 0 or part5.Volume == 0 or part6.Volume == 0 then
			part.Volume = 0.5
			part2.Volume = 0.5
			part3.Volume = 0.5
			part4.Volume = 0.5
			part5.Volume = 0.5
		part6.Volume = 0.5
		script.Parent.Image = "http://www.roblox.com/asset/?id=5989509343"
		else
			part.Volume = 0
			part2.Volume = 0
			part3.Volume = 0
			part4.Volume = 0
			part5.Volume = 0
		part6.Volume = 0
		script.Parent.Image = "http://www.roblox.com/asset/?id=5990231900"
	end	
end)
local part = game.Workspace.SoundRegions2.Part
local part2 = game.Workspace.SoundRegions2.Part2
local part3 = game.Workspace.SoundRegions2.Part3
local part4 = game.Workspace.SoundRegions2.Part4
local part5 = game.Workspace.SoundRegions2.Part5
local part6 = game.Workspace.SoundRegions2.Part6

script.Parent.MouseButton1Click:Connect(function()
		if part.Volume == 0 or part2.Volume == 0 or part3.Volume == 0 or part4.Volume == 0 or part5.Volume == 0 or part6.Volume == 0 then
			part.Volume = 0.5
			part2.Volume = 0.5
			part3.Volume = 0.5
			part4.Volume = 0.5
			part5.Volume = 0.5

                        part.Playing = true
			part2.Playing = true
			part3.Playing = true
			part4.Playing = true
			part5.Playing = true

		part6.Volume = 0.5
		script.Parent.Image = "http://www.roblox.com/asset/?id=5989509343"
		else
			part.Volume = 0
			part2.Volume = 0
			part3.Volume = 0
			part4.Volume = 0
			part5.Volume = 0

                        part.Playing = false
			part2.Playing = false
			part3.Playing = false
			part4.Playing = false
			part5.Playing = false

		part6.Volume = 0
		script.Parent.Image = "http://www.roblox.com/asset/?id=5990231900"
	end	
end)

Edit: for our collective sanity, please use objects

Doesn’t work, when you go to unmute it after you’ve muted it, it plays all of the songs at the same time. Also, what do you mean objects?

I am not too sure what your specific contextual use case is, however, I got you covered for objects. They’re tables in lua: Tables | Documentation - Roblox Creator Hub

Thanks, I’ll take a look at it. Basically, I’ve made a Music On/Off button it works however when the song repeats (because the song is looped) it’ll start to play again.

Yes, I highly recommend using objects and iterating through it. The method that you are using will not scale well on mass use case.

How would I convert them into tables? If you don’t mind can you show me an example or explain? I’ve read the actual tables documentation but I don’t know how I’d apply it to this situation specifically.

So, have a table full of sound ids. And also have an index variable. Every time the sound end event fires, increase the index by one and set the sound Id to the one in the table. Repeat this until you are at the end. However, you can indeed pause the sound and unpause it without breaking the system. I am on my iPad,if I were on my pc, I would have just given you the fully functional code. Plus, my mom’s calling for dinner :laughing:

1 Like

I will give you the code tomorrow if possible,

1 Like

I’ve changed up my code a bit, still not working though. Here’s the new code I came up with, but still not working like I said. (bump)

local status = script.Value
local on = status.Value == "On"
local off = status.Value == "Off"
local player = game.Players.LocalPlayer
	
script.Parent.MouseButton1Click:Connect(function()
	if on == "On" then
		print("On!")
		local soundregions = game.Workspace.SoundRegions2:GetChildren()
		for i,v in pairs (soundregions) do
			v.Volume = 0
		end
		status.Value = "Off"
	elseif on == "Off" then
		print("Off!")
		local soundregions = game.Workspace.SoundRegions2:GetChildren()
		for i,v in pairs (soundregions) do
			v.Volume = 0.5
		end
		status.Value = "On"
	end
end)

if I am undestanding you right this should stop the sounds where they are then restart them after you unmute

local status = script.Value
local on = status.Value == "On"
local off = status.Value == "Off"
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if on == "On" then
		print("On!")
		local soundregions = game.Workspace.SoundRegions2:GetChildren()
		for i,v in pairs (soundregions) do
			v.Volume = 0
			v.PlaybackSpeed = 0 -- will make the sound stop playing but stay in play status(Pauses it)
		end
		on = 'Off'
		status.Value = "Off"
	elseif on == "Off" then
		print("Off!")
		local soundregions = game.Workspace.SoundRegions2:GetChildren()
		for i,v in pairs (soundregions) do
			v.Volume = 0.5
			v.PlaybackSpeed = 1  -- unpause the sound
		end
		on = 'On'
		status.Value = "On"
	end
end)

oh nice work on changing this over to a table of objects and cleaning up the code

1 Like

That didn’t work so I’ll re-explain. Basically I’m trying to make it so when you press the button the sound mutes and when you press it again it unmutes.

are the sounds not playing at all now or not muting?

They aren’t muting at all, unfortunately. No error’s in the output.

I did fix something in the above script where you were not setting the on variable for the change

Still doesn’t seem to be working. I’ll send you what the Explorer looks like even though everything should be fine with that.

do you have the sounds checked for playing?
https://gyazo.com/c1a6683fb89024743435c1238048c9b6

Explorer1 Explorer2

Yep, I’ve checked. Still says playing when I go and press the mute button

ok i think the problem you are having here is a roblox BUG…
it seems for some unknown reason if a sound is set to looping and also set to playing it will double play when you hit play in studio atleast where i am testing in the ui

so i think to fix this you need to uncheck Playing on all the sounds then just fire play on them when the script starts

I will edit the script above to have the initial for loop to make them play – also you can use a ipairs loop if you wanted them to be changed in order

1 Like

The script you’ve re-edited again seems to play the songs all at once.