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

Sound making clones of itself in workspace?

I have a slight problem with this script. Whenever the sound plays, it duplicates itself repeatedly, and I'm trying to get this to only have one instance of itself in workspace.

while true do wait(1)
local library = game.ServerStorage.Sounds:GetChildren()
	for i = 1, #library do
if library[i]:IsA("Sound") then
		game:GetService("ContentProvider"):Preload(library[i].SoundId)
	end
end

local Ambient = Instance.new("Sound")
Ambient.Parent = game.workspace
Ambient.SoundId = "rbxassetid://238895410"
Ambient:Play()
Ambient.Volume = 1
end

2 answers

✓ Accepted answer

You have a simple problem. When you create your sound, it is inside of the while true do loop. So, every second or so, their will be a new sound created into Workspace. It's a very simple problem to fix as well!

If you only will be having 1 sound in your Workspace at a time, then you should just make a check to see if Sound exists in the Workspace or not, then do coding.

while true do wait(1)
local library = game.ServerStorage.Sounds:GetChildren()
    for i = 1, #library do
if library[i]:IsA("Sound") then
        game:GetService("ContentProvider"):Preload(library[i].SoundId)
    end
end
if game.Workspace:FindFirstChild("Sound") == nil then
	local Ambient = Instance.new("Sound")
	Ambient.Parent = game.workspace
	Ambient.SoundId = "rbxassetid://238895410"
	Ambient:Play()
	Ambient.Volume = 1
	end
end

As you can see, what I did was very simple and you probably just overlooked it. However, if you'll end up having multiple Sound within your Workspace, you should change their name and do around the same method I showed above.

Accept and upvote if this helps! Comment Questions bellow!

This problem could have been avoided if this code is more organized!

while true do
	wait(1)
	local library = game.ServerStorage.Sounds:GetChildren()
	    for i = 1, #library do
	if library[i]:IsA("Sound") then
	        game:GetService("ContentProvider"):Preload(library[i].SoundId)
	    end
	end
	local Ambient = Instance.new("Sound")
	Ambient.Parent = game.workspace
	Ambient.SoundId = "rbxassetid://238895410"
	Ambient:Play()
	Ambient.Volume = 1
end

As you can see, the chunk of code where Ambient is at is wrapped in a "while" loop. That's why a new sound object is being made every second. Just move it.

while true do
	wait(1)
	local library = game.ServerStorage.Sounds:GetChildren()
	    for i = 1, #library do
	if library[i]:IsA("Sound") then
	        game:GetService("ContentProvider"):Preload(library[i].SoundId)
	    end
	end
end

local Ambient = Instance.new("Sound")
Ambient.Parent = game.workspace
Ambient.SoundId = "rbxassetid://238895410"
Ambient:Play()
Ambient.Volume = 1

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