How to reparent multiple instances into multiple instances

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
Screenshot 2023-08-24 070938

After
Screenshot 2023-08-24 071020

As you can see, all I need is the Luau code version of Ctrl+Shift+V

2 Likes

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()
1 Like

Alright I made the table. But my main challenge is really the second part. How do I refer to the cloned objects, let alone paste them into each sound?

2 Likes

I updated my post, see there
Char

1 Like

The code looks fine to me? I can’t see what other things you want to achieve.

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

1 Like

Lol

The code doesn’t even work because the for loop I crafted is useless

If ALL of your effects are stored in the indoors folder, then you can simply use the same piece of code i used for getting the audios

Edit: i edited the message now, that should do it for you

1 Like

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

Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.