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

How to delete duplicate instances?

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

✓ Accepted answer

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

2 comments

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

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.

local sound = game.Workspace:findFirstChild("Sound")
if sound then
	sound:remove()
end)
--Clone your sound into the workspace on this line.

Hope that helps!

1 comment

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

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