Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to delete duplicate instances?

Asked by
Scerzy 85
9 years ago

I have a GUI in which when you click a button, a sound gets placed into the workspace. However, if you keep clicking that button, the sound will get spammed. Can someone point me in the right direction? I think the script would be something like

if game.Workspace.sound ---- Something to detect that theres more than one sound
then game.Workspace.sound:remove()

Sorry if that's not right, but that's what I came up with off the top of my head

2 answers

Log in to vote
1
Answered by 9 years ago

This is quite a simple process to accomplish. You're going somewhat the correct direction. What you want to do when you click the GUI that is creating the Sound is have it check if a sound exists BEFORE the sound is even created. This is what a script would look like if you were just checking.

if game.Workspace:FindFirstChild("Sound") ~= nil then
    print("Sound Already Exists!!")
else
    s = Instance.new("Sound", game.Workspace)
end

Now, I used FindFirstChild() because an error may occur if you go to something that is Nil !

Here is what it may look like in a GUI, but beware that my hierarchy will differ from yours!

game.StarterGui.ScreenGui.TextButton.MouseButton1Click:connect(function()
    if game.Workspace:FindFirstChild("Sound") ~= nil then
        print("Already Exists")
else
        s = Instance.new("Sound", game.Workspace)
        -- Name the sound, Add the asset link, etc bellow
    end
end)

If this helps, UpVote and Accept! Ask any questions if needed

0
The code I did is just not making another sound, instead it will see if it's there and if so, print above. The sound doesn't get removed in this script alphawolvess 1784 — 9y
0
Remember, anything done in StarterGui will NOT replicate to the player! StarterGui is simply a holder to store GUIs that will later be cloned into your PlayerGui. Any events connected or visual changes must therefore be done in PlayerGui. Perci1 4988 — 9y
Ad
Log in to vote
0
Answered by 9 years ago
local sound = game.Workspace:findFirstChild("Sound")
if sound then
    sound:remove()
end)
--Clone your sound into the workspace on this line.

Hope that helps!

0
The problem with this is people type the sound ID they want, so I cant clone it without knowing. It's a music GUI Scerzy 85 — 9y

Answer this question