Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you remove one Sound instance if there's more then 1 in workspace?

hi, I've made this script, But I want to delete any extra sound instances. there should be a maximum of 1 Sound instance.

local Clicker = script.Parent
function onClick()
	local Music = Instance.new("Sound", game.Workspace)
    Music.SoundId = "https://www.roblox.com/asset/?id=229710755"
    Music.Looped = false
    Music.Archivable = false
    wait(0)
    Music:Play()
    wait(120)
    Music:Stop()
	if game.Workspace.Sound == true then
		game.Workspace.Sound:Remove()
	end
end
Clicker.ClickDetector.MouseClick:connect(onClick)

any help will be appreciated.

3 comments

1

The script works, change line 11 to (if game.Workspace:FindFirstChild("Sound") then)

0

That didn't work, but thanks for helping

0

Oh, if there is more than one, I didn't read the whole thing

4 answers

Instead of deleting your old music and adding in a new one, insert a sound object and change the sound ID when you want to change the music and then use the Play function/method.

local Music = Instance.new("Sound", workspace)
if workspace:FindFirstChild("SpecialMusic") then
	Music:Destroy()
else
	Music.Name = "SpecialMusic"
end
Music.Looped = false

script.Parent.ClickDetector.MouseClick:connect(function(plyr)
	Music.SoundId = "https://www.roblox.com/asset/?id=229710755"
	Music:Play()
	wait(120)
	Music:Stop()
end)

If there are already other sound IDs in the game:

temporarily = true --If this is true, then all other playing music in workspace is going to pause then play again. If it is set to false, the music will be off FOREVER!

function revive(audio)
	wait(120)
	sound:Play()
end

script.Parent.ClickDetector.MouseClick:connect(function(plyr)
	local Music = Instance.new("Sound", workspace)
	Music.SoundId = "https://www.roblox.com/asset/?id=229710755"
	Music.Looped = false
	for _,sound in pairs(workspace:GetChildren()) do
		if sound:IsA("Sound") and sound ~= Music then
			if temporarily then
				revive(sound)
			else
				sound:Destroy()
			end
		end
	end
	Music:Play()
	wait(120)
	Music:Destroy()
end


Hope this helps! If there is something that is not right please tell me!

Thanks for helping, but unfortunately none of your scripts worked like the way I wanted them too, so I created a new part which Mutes the sound and removes it. again thanks for all your help

insert this:

v = game.Workspace:GetChildren() if v:IsA("Sound") then v:Destroy() end

1 comment

0

I'm sorry if that doesn't work, I usually don't play with sounds

Okay so the function only runs once right? So this means it will only remove the first Sound it finds, what you might want to do is use i, v in pairs() loop. So it can look through all the workspace children and if there is a sound it will destroy it.

If you have no experience with in pairs loop i'd recommend you to check the wiki.

for i, v in pairs(game.Workspace:children()) do
    if v:IsA("Sound") then
        v:Remove()
    end
end
Clicker.ClickDetector.MouseClick:connect(onClick)

Hope this helped!

1 comment

0

It somewhat worked, I want it so if there is more than 1 then it removes the extra sounds, I need it so there is a maximum of 1 sound playing

This discussion is preserved as it was. New answers, comments, and votes are closed.