Hello, this is the first problem I’ve had so much trouble solving that I’ve needed to ask on the DevForum.
Basically, I know how to use for loops to clone all of an Instance’s children, and how to reparent them to one location. However, I am wanting to use a for loop to clone all an Instance’s children, then basically Ctrl+Shift+V them into multiple Instances. I have searched tirelessly for a solution but have found none.
Here is a simplified version of my code with the problematic area highlighted:
local player = script.Parent
local indoors = player:WaitForChild("indoors")
local muffle = script.mufflesuccess
local gamefx = game.Workspace.GameFX
if not player:FindFirstChild("Humanoid") then return end
wait(5)
while true do
wait(0.1)
if indoors.Value == true then
print("isindoor")
if muffle.Value == false then
-- THIS PART IS THE PROBLEM(below)
for i, v in pairs(gamefx.Sounds:GetDescendants()) do
if v:IsA("Sound") then
for _, r in pairs(gamefx.Sounds.FX.indoor:GetChildren()) do
if r.Name ~= "what??" then
local indoorclone = r:Clone()
indoorclone.Parent = v
end
end
end
end
-- THIS PART IS THE PROBLEM(above)
muffle.Value = true
end
else
if muffle.Value == true then
for i, v in pairs(gamefx.Sounds:GetDescendants()) do
if v:IsA("Sound") then
for i, j in pairs(v:GetChildren()) do
if gamefx.Sounds.FX.indoor:FindFirstChild(j.Name) then
j:Destroy()
end
end
end
end
muffle.Value = false
end
end
end
What I want to achieve with this script:
Before
After
As you can see, all I need is the Luau code version of Ctrl+Shift+V
Well you could get every sound, then put them in a table, so you can easily loop trough the table with the sounds and add you effects:
local Sounds = {}
function GetSounds()
for i, sound in pairs(SoundsFolder:GetDescendants) do
if sound:IsA(“Sound”) then
table.insert(Sounds, sound)
end
end
GetSounds()
function PasteFx()
for i, sound in pairs(Sounds) do
for _, effect in pairs(indoors:GetChildren()) do
effect:Clone().Parent = sound
end
end
end
PasteFx()
Ok we’re almost there, but I know already how to do what you’ve done by referencing each effect individually
fx1
fx2
Then reparenting the clones.
The problem with this is every time I add a new indoor effect, I need to update the script to reference it, which is tedious.
Is there any method where it would work just as well if there were a
fx4? fx5?
By accounting for all children of the folder “indoors”?
If you find no way of doing this, I will wait a few weeks to see if anyone else knows how do this. If there is no response I’ll mark yours as the solution
Perfect, works wonderfully by using simple tables.
For the record, this is the working script with your adjustments made:
local player = script.Parent
local indoors = player:WaitForChild("indoors")
local muffle = script:WaitForChild("mufflesuccess",10)
local gamefx = game.Workspace.GameFX
local effects = {}
if not player:FindFirstChild("Humanoid") then return end
wait(5)
while true do
local sounds = {}
wait(0.1)
if indoors.Value == true then
print("isindoor")
if muffle.Value == false then
-- THIS PART IS THE PROBLEM(below)
for i, v in pairs(gamefx.Sounds:GetDescendants()) do
if v:IsA("Sound") then
table.insert(sounds, v)
for i, sound in pairs(sounds) do
for _, effect in pairs(gamefx.Sounds.FX.indoor:GetChildren()) do
effect:Clone().Parent = sound
end
end
end
end
-- THIS PART IS THE PROBLEM(above)
muffle.Value = true
end
else
if muffle.Value == true then
for i, v in pairs(gamefx.Sounds:GetDescendants()) do
if v:IsA("Sound") then
for i, j in pairs(v:GetChildren()) do
if gamefx.Sounds.FX.indoor:FindFirstChild(j.Name) then
j:Destroy()
end
end
end
end
muffle.Value = false
end
end
end