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
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
local sound = game.Workspace:findFirstChild("Sound") if sound then sound:remove() end) --Clone your sound into the workspace on this line.
Hope that helps!