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.
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
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!