Copying folders

So I’m trying to make a regeneration system and I was wondering how I copy all the folders in workspace and copy everything that’s inside of them aswell. I would appreciate the help, thanks!

All you do is use :Clone()

https://developer.roblox.com/en-us/api-reference/function/Instance/Clone

local Folder = game.workspace.Folder
local Clone = Folder:Clone()
Clone.Parent = workspace
Clone.Part:Destroy() -- lets say you want to remove a part in the folder on clone

You would use :Clone() to clone the folders. It will also clone all it’s children I believe though I’m unsure.

for _, folder in pairs(workspace:GetChildren()) do
   if folder and folder:IsA("Folder") then
      local FolderCopy = folder:Clone()
      FolderCopy.Parent = workspace -- This will copy the folder back into workspace
   end
end

There is a little problem it only copies some of the folder and got all of them and this is randomized every time is there a way to fix this?

Are the folders all in workspace? If so there shouldn’t be any issues with it.

The only issue I can think off is that this is running as soon as the game starts which means the folders may not be loaded in time. To test this is add a wait and see if everything works after the wait.

task.wait(10) -- wait 10 seconds
for _, folder in pairs(workspace:GetChildren()) do
   if folder and folder:IsA("Folder") then
      local FolderCopy = folder:Clone()
      FolderCopy.Parent = workspace -- This will copy the folder back into workspace
   end
end

Weird it still does the same thing

Your can create an new Folder Instance and copy all the Items inside the old folder into the new Folder, just like this:

local NewFolder = Instance.new("Folder")

for Index, Item in pairs(YourFolderRoute:GetChildren()) do
     local ItemCopy = Item:Clone()
     ItemCopy.Parent = NewFolder
end
  • Then u can move the new folder wherever you want

Are any of the folder’s descendants instantiated by the client? Because if they are then they won’t exist on the server.

I do it server side so I don’t know why this is happening but I’m thinking it has to do with timing out because it stops at a certain point.

Sorry I was gone for a while but I think the reasoning is that its copying large amount of items at one time. Is there any way I can get around this?