So I'm trying to make a script that loads maps by chunks, instead of just parenting the entire thing to workspace (these maps are fairly huge). I've come to this conclusion so far:
-- Arguments:
-- 'map': The model
-- 'chunk': How many children are loaded in per 'rate'
-- 'rate': The rate at which chunks are loaded
local function loadMapByChunks(map,chunk,rate)
-- iterate through model's children
local objects={}
local left={}
local last=map
while last do
local children=last:children()
for i = 1,#children do
local v=children[i]
objects[#objects+1]={v,v.Parent}
v.Parent=nil
end
last = left[#left]
left[#left] = nil
end
queue=nil
map.Parent=workspace -- map container, but consists of no children
-- load the children to the map in chunks
for i=1,#objects do
local v=objects[i]
v[1].Parent=v[2]
if i%chunk==1 then
print("Chunks loaded: "..i)
wait(rate)
end
end
-- end result
print("Finished loading chunks. Total objects: "..(#objects))
end
Now before you go trying to look for problems, this code is actually exceptional to a certain point. However for some reason I'm not 100% satisfied with it, as I feel like it's somewhat 'fake'.
Sometimes I'll check and see how many instances are actually being loaded accordingly to the rate variable, and see that sometimes a few extra instances are loading per child.
Personally I think it's because some nil parented children are parenting themselves to more nil parented children, and then when that one nil parented child is parented to workspace, all of it's descendants follow (going over the chunk limit boundary per rate).
If anyone thinks there's a better way to do this, or find a way to solve this current problem, I'd very much appreciate it. Thanks!
