So what I’m doing is I’m trying to load a map from ServerStorage to Workspace through a for loop. I’m doing this because I want to prevent lag spikes when large maps are loaded in.
So what I’m doing is I have a map as a model in ServerStorage and I want to move it over to workspace. I start off by creating a Model instance in Workspace and have all the parts, models, and such transported into that model from the map in ServerStorage using :GetChildren() in a for loop, meaning all the instances are being transported one at a time. And in the end, I want it to be an exact replica, all the models are in the same place, all the parts have the same name, etc.
So my problem is, what if there’s a model inside a model? Say the map is a model and inside there’s a model too and inside that model, there’s another model, and so on. (Here’s an example)
When I iterate through a model (say being the map) and run across another model (and lets say this model has 500-1k parts in it) it’ll load all the parts at once in that model and cause a lag spike.
So my question is, how can I transfer the map to workspace regardless of the model chains (a model inside a model) and it being the exact same as the model in ServerStorage, like everything has the same parents and such.
Also sorry if this explanation is garbage, I tried my best.
Well yeah but I want everything to have the same parents, if I use :GetDescendants() then it’ll transfer all the parts and stuff into the model in workspace but they’ll lose their parents.
local mapToClone = game.ServerStorage.SomeMap
function cloneChildren(model, newParent)
for _, v in pairs(model:GetChildren()) do
local newV = v:Clone()
newV.Parent = newParent
if #(v:GetChildren()) > 0 then
cloneChildren(v, newParent)
end
end
end
cloneChildren(mapToClone, workspace)
Yeah, that somewhat worked. Instances like fire or Point light were being put in the model in workspace though instead of the parent they were with but other than that it’s pretty good
Hey, I worked with the code a little bit this morning and I fixed the issues I was having
local mapToClone = game.ServerStorage.lol_TrainStation
function cloneChildren(model, newParent)
for _, v in pairs(model:GetChildren()) do
if v:IsA("Model") or v:IsA("Folder") then
local newModel = Instance.new("Model", newParent)
newModel.Name = v.Name
cloneChildren(v, newModel)
else
local newV = v:Clone()
newV.Parent = newParent
end
wait()
end
end
cloneChildren(mapToClone, workspace.Folder)
I fixed where say point lights were going into the destination folder instead of their parent model
Again, credit to @Xsticcy for the script, all I did was just fix it