Trouble Cloning sounds into parts

I have this idea to automatically clone sounds from a folder and place them into “sensors” (basically parts). This saves me having to go through every single one individually to change Ids or whatnot.

I’ve tried this from someone but it doesn’t work.

local SoundClones = script.Parent.Sounds:GetChildren()

local ClunkSensors = script.Parent:GetDescendants()

function clone()

for i = 1, #ClunkSensors do

local clone = SoundClones

for i = 1, #SoundClones do

clone.Parent = ClunkSensors[i]

end

end

end

clone()

any help will be appreciated :slight_smile:

1 Like

Is this something you want to happen on during runtime or just so you don’t have to manually place it? If it’s the latter the command bar is your friend!

Either way, this code should work:

local SoundClones = script.Parent.Sounds:GetChildren()

local ClunkSensors = script.Parent:GetDescendants()

for _, Sensor in pairs(ClunkSensors) do
      for _, Sound in pairs(SoundClones) do
      local sound = Sound:Clone()
      sound.Parent = Sensor
      end
end

From what I gather, SoundClones is also a descendant of ClunkSensors? So you’re going to end up with extra clones from what I can guess.

Another opinion, if you’re going to be having so many copies (of the same sound) it would probably be better performance wise to have just once sound instance playing on the client, which you can then parent to whichever source part is closest. Although that would only really work if you’re meant to hear one sound at a time.

1 Like

Thank you for your reply. I’ve just tried this and I did get extra clones inside the cloned sounds themselves. Any idea on how I can fix this?

1 Like

Try this if you only want one sound in each part:

local SoundClones = script.Parent.Sounds:GetChildren()
local ClunkSensors = script.Parent:GetDescendants()

function clone()
	for i = 1,#ClunkSensors do
		local sound = SoundClones[i] or SoundClones[1]
		if sound then
			sound:clone().Parent = ClunkSensors[i]
		end
	end
end

clone()

Also, what @Natisb said about the extra clones is correct. If you are cloning the same sound into each part it would be better just to do:

local Sound = script.Parent.Sounds.Sound
local ClunkSensors = script.Parent:GetDescendants()

function clone()
	for _,sensor in pairs(ClunkSensors) do
		local newSound = Sound:clone()
		newSound.Parent = sensor
	end
end

clone()

What I meant was the script copies all of the sounds in this folder

image

and puts them into these bricks

image

Edit: I solved it. I modified part of the script to add the following:

if Sensor:IsA('Part' then

1 Like