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()
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.
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()