Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Better way to load chunks?

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!

1 answer

✓ Accepted answer

I am a little unsure of what you mean by seeing extra instances. I believe what you are referring to is print() on line 31. Let's say you make chunk 100, the output would be something like

Chunks loaded: 1

Chunks loaded: 101

Chunks loaded: 201

etc... If the extra instances problem is that 1 on the end then I'd suggest changing line 30 to if i%chunk==0 then

Next it only looks like children of the map is chunk loaded and no further descendants. If you want those descendants loaded the same say then I think you can just add a line after 16: left[#left+1] = v

This discussion is preserved as it was. New answers, comments, and votes are closed.